- ติดตั้ง Package ผ่าน NuGet
- สร้างไฟล์ log4net.config
- บอก log4net ให้โหลดไฟล์คอนฟิก
- How to use Log4Net with ASP.NET Core for logging | dotnetthoughts
- Does Log4Net Work With .NET 6? – Loupe (onloupe.com)
1.ติดตั้ง Package ผ่าน NuGet
PM> Install-Package log4net -Version 2.0.13
PM> NuGet\Install-Package log4net -Version 2.0.15
> dotnet add package log4net --version 2.0.15
2.สร้างไฟล์ log4net.config
สร้างไฟล์ log4net.config ไว้เก็บค่า config เกี่ยวกับ log4net
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> </appender> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <file value="log/" /> <datePattern value="yyyy-MM-dd.'log'"/> <staticLogFileName value="false"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <maxSizeRollBackups value="100"/> <maximumFileSize value="15MB"/> <encoding value="UTF-8"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p%d{ yyyy-MM-dd HH:mm:ss} - %m %n" /> </layout> </appender> <root> <level value="ALL"/> <appender-ref ref="console" /> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net>
หรือ
<?xml version="1.0" encoding="utf-8" ?> <log4net> <root> <level value="All" /> <appender-ref ref="console" /> <appender-ref ref="file" /> </root> <appender name="console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level %method - %message%newline" /> </layout> </appender> <appender name="file" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <file value="logs/" /> <datePattern value="yyMMdd'_C5.log'" /> <staticLogFileName value="false" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <encoding value="UTF-8"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p%d{ HH:mm:ss} li:%line - [%method] %m %n" /> </layout> </appender> </log4net>
และที่ “Copy to Output Directory” ให้เลือกเป็น “Copy if newer”
3.บอก log4net ให้โหลดไฟล์คอนฟิก
.NET 5 แก้ไขไฟล์ Program.cs
using System.IO; using System.Reflection; using System.Xml; namespace ConsoleApp5 { class Program { public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); static void Main() { XmlDocument log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead("log4net.config")); var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]); log.Info("Hello World!"); } } }
.NET 6 แก้ไขไฟล์ Program.cs
using System.Reflection; using System.Xml; namespace ConsoleApp6; class Program { public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); static void Main() { XmlDocument log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead("log4net.config")); var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]); log.Info("Hello World!"); } }
.NET 6 new program style
// See https://aka.ms/new-console-template for more information using System.Reflection; using System.Xml; log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); XmlDocument log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead("log4net.config")); var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]); log.Info("Hello World!");