Log4net.dll is an assemble files that used to do logging.
We can use this to output our message to customized path.
This also can make us avoiding that conflict in multiple-threading writing.
How to use Log4net.dll as i want that describe in this article title?
1. plan our configure file:
<configure>
<configSections> name="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0" </configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppenderINFO" />
</root>
<-- add loggers here as you want-->
<appender name="LogFileAppenderINFO" type="log4net.Appender.FileAppender" >
<File type="Log4net.Util.PatternString" value="C:\Script\%property{FileName}.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</log4net>
</configure>
2. Save the config file and add this assembly to your project's AssemblyInfo.cs
3. How to use it in our codes?
Initial the %property{FileName}before create our Loggers.
example:
Log4net.GlobalContext.Properties["FileName"] = "filename";
Log4net.ILog myILog = Log4net.LogManager.GetLogger( System.reflection.MethodBase.GetCurrentMethod().DeclaringType);
Then, we can use our "myILog" to output our message to our special file.
example:
myILog.INFO("message");
Do you note the read character?
Why i highlight them, because i make a mistake on them.
I make wrong on "File" and "type".
It is case sensitive!!!!!!