19
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

log4net覚え書き

Posted at

ログ出力ライブラリであるlog4netを使うまでの準備をまとめる。

  • Apacheのサイトからlog4netのバイナリをダウンロード。
  • 本投稿ではバージョン1.2.13 newkeyを使用。
  • 任意のフォルダに展開。
  • Visual Studioのプロジェクトにおいて、参照設定でdllを選択。
  • AssemblyInfo.csにて次の記述を追加。ログ設定ファイル名の指定。
  • C#のプロジェクトを仮定。
AssemblyInfo.cs
// Log4net用設定ファイル
[assembly: log4net.Config.XmlConfigurator(ConfigFile=@"Log4net.xml", Watch=true)]
  • ログ出力フォーマットファイルを用意。
Log4net.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<log4net>
		<appender name="MylogAppender" type="log4net.Appender.RollingFileAppender">
			<File value="LogSample.log" />
			<AppendToFile value="true" />
			<RollingStyle value="Size" />
			<maxSizeRollBackups value="10" />
			<maximumFileSize value="5KB" />
			<StaticLogFileName value="true" />
			<layout type="log4net.Layout.PatternLayout">
				<ConversionPattern value="%date [%thread] [%-5level] (%method) - %message%n" />
			</layout>
		</appender>

		<root>
			<level value="INFO" />

			<appender-ref ref="MylogAppender" />
		</root>
	</log4net>
</configuration>
  • ロガーを提供するクラスを追加。
Log.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

    public class Log
    {
        private static ILog logger = null;

        public static ILog getLogger()
        {
            if (logger == null)
            {
                logger = LogManager.GetLogger(@"LogSample");
            }

            return logger;
        }
        
    }
  • ログを出力したいクラスにて、上のロガー提供クラスからロガーを入手。
  • そしてログ出力。
Program.cs
using log4net;

    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            ILog log = Log.getLogger();

            log.Info("===== Program Start");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            log.Info("===== Program End");
        }
    }
Form1.cs
using log4net;

    public partial class Form1 : Form
    {
        private ILog logger;

        public Form1()
        {
            InitializeComponent();

            logger = Log.getLogger();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            logger.Info("Button Clicked!");
        }
    }
  • ログ出力例
LogSample.log
2015-03-12 01:08:12,495 [1] [INFO ] (Main) - ===== Program Start
2015-03-12 01:08:14,792 [1] [INFO ] (button1_Click) - Button Clicked!
2015-03-12 01:08:15,034 [1] [INFO ] (button1_Click) - Button Clicked!
2015-03-12 01:08:15,760 [1] [INFO ] (Main) - ===== Program End
19
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?