目次
1.Nugetパッケージのインストール
log4netをインストールしたいプロジェクトを右クリックする



Nugetパッケージの管理でlog4netを検索する


インストール


参照にlog4netが追加されていることを確認する

2.AssemblyInfoの編集
※プログラム内で設定ファイルを指定する場合はこれは実行しなくてもよい


AssemblyInfo.csの末尾に以下のコードを追加する
[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"設定ファイル名", Watch = true)]
3.設定ファイルの作成




作成したテキストファイルをAssemblyInfo.csで指定した設定ファイル名に変更する




4.設定ファイルの出力設定(忘れがちなので注意!)
コンパイル時、設定ファイルが出力としてコピーされるようにする
※設定しない場合は設定した場所に手動で設定ファイルを置く



5.作成した設定ファイルに設定を書く
設定ファイルに以下の文言を記載すればとりあえず動く
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="log" type="log4net.Appender.RollingFileAppender">
<appendToFile value="true" />
<StaticLogFileName value="false" />
<rollingStyle value="Date" />
<file value="log" />
<DatePattern value='yyyyMMdd".log"' />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="%date [%-5level] - %message%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="log" />
</root>
</log4net>
</configuration>
6.コードを書く
以下の文言を記載すればとりあえず動く
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Program
{
private static log4net.ILog logger = log4net.LogManager
.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
logger.Info("実行されたよ");
}
}
}