如果我們開發的windows service要如何設定log4net來正常運作呢?
請依以下步驟實作
1. 在 AssemblyInfo.cs 加入以下語法:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4net.config", Watch = true)]
2. Log4net.Config
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="C:\logs\winservice.log"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock,log4net" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="2" /> <maximumFileSize value="1MB" /> <staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root> <level value="ALL" /> <appender-ref ref="LogFileAppender" />
</root>
</log4net>
3. C# 程式宣告
private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(class_name));
4. 寫log
_log.Info(“Info”);
_log.Warm(“Warm”);
沒有留言:
張貼留言