How to variable path of log output via log4net

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!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值