0

我想在代码中而不是在配置文件中设置 Rolling Appender 动态。所以我有一个函数会返回 log4net.ILog

    <Assembly: log4net.config.XmlConfigurator(ConfigFile:="Log4Net.config", Watch:=True)> 
Public Function genLogXMLCfg(cls As Object) As log4net.ILog
    Dim clsName As String = cls.ToString()
    Dim rollAppen As New RollingFileAppender()

    rollAppen.Name = clsName + "Appender"
    rollAppen.RollingStyle = RollingFileAppender.RollingMode.Composite
    rollAppen.File = "logs\\"
    rollAppen.DatePattern = "'" + clsName.ToLower() + "'" + "yyyyMMdd'.log'"
    rollAppen.LockingModel = New log4net.Appender.FileAppender.MinimalLock()
    rollAppen.AppendToFile = True
    rollAppen.StaticLogFileName = False
    rollAppen.MaxSizeRollBackups = 3
    rollAppen.MaximumFileSize = "100KB"
    Dim layout As New log4net.Layout.PatternLayout("%d [%t] %-5p %c (line:%L) - %m%n")
    rollAppen.Layout = layout
    layout.ActivateOptions()
    rollAppen.ActivateOptions()

    Dim logg As ILog = LogManager.GetLogger(clsName)
    Dim l As log4net.Repository.Hierarchy.Logger = DirectCast(logg.Logger, log4net.Repository.Hierarchy.Logger)
    l.AddAppender(rollAppen)
    l.Repository.Configured = True
    Return logg      
End Function

因为我想从代码动态生成滚动附加程序,但同时我想从配置文件设置记录器,所以我有以下配置文件调用(Log4Net.config)

   <configSections>
       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />     
   </configSections>
   <appSettings>    
    <add key="log4net.Config" value="log4net.config"/>
    <add key="log4net.Config.Watch" value="True"/>
   </appSettings>
<log4net>
    <logger name="ConsoleApplication1.LogTest2">
        <level value="WARN" />      
    </logger>
    <logger name="ConsoleApplication1.testCls">
        <level value="INFO" />
    </logger>
</log4net>

现在我通过以下代码测试该功能:

       Dim cls As LogTest2 = New LogTest2()
    Dim tCls As testCls = New testCls()
    Dim l4n As log4net.ILog
    Dim l4n2 As log4net.ILog

    l4n = cls.genLogXMLCfg(tCls.GetType().ToString())
    l4n2 = cls.genLogXMLCfg(cls.GetType().ToString())

    l4n.Debug("... Here is a debug log -2.")
    l4n.Info("... and an Info log.")
    System.Threading.Thread.Sleep(3000)
    l4n.Warn("... and a warning 1.")
    l4n.Debug("... Here is a debug log -1.")
    l4n.Warn("... and a warning 2.")
    l4n.Warn("... and a warning 3.")
    l4n2.Warn("l4n2 cls and a warning -1.")
    l4n.Warn("... and a warning 4.")
    l4n.Warn("... and a warning 5.")
    Console.Write(" ... ... ... ")
    System.Threading.Thread.Sleep(3000)
    l4n.Warn("... and a warning 6.")
    l4n.Debug("... Here is a debug log 1.")
    l4n.Warn("... and a warning 7.")
    l4n2.Debug("l4n2 cls Here is a debug log.")
    l4n2.Info("l4n2 cls and an Info log.")
    l4n.Fatal("... and a fatal .")
    l4n2.Fatal("l4n2 and a fatal .")
    l4n.Debug("... Here is a debug log 2.")
    l4n.Warn("... and a warning 8.")
    l4n.Error("... and an error.")
    l4n.Debug("... Here is a debug log 3.")
    l4n.Fatal("... and a fatal .")
    l4n.Debug("... Here is a debug log 4.")
    l4n2.Debug("l4n2 cls Here is a debug log.")
    l4n2.Info("l4n2 cls and an Info log.")
    l4n2.Warn("l4n2 cls and a warning.")
    l4n2.Error("l4n2 cls and an error.")
    l4n2.Fatal("l4n2 cls and a fatal .")

之后,在日志文件夹调用中生成了两个日志文件

**consoleapplication1.logtest220130610.log** and **consoleapplication1.testcls20130610.log**
The content are:

**consoleapplication1.logtest220130610.log**
2013-06-10 18:41:38,802 [9] WARN  ConsoleApplication1.LogTest2 (line:182) - l4n2 cls and a warning -1.
2013-06-10 18:41:41,819 [9] FATAL ConsoleApplication1.LogTest2 (line:193) - l4n2 and a fatal .
2013-06-10 18:41:41,831 [9] WARN  ConsoleApplication1.LogTest2 (line:206) - l4n2 cls and a warning.
2013-06-10 18:41:41,833 [9] ERROR ConsoleApplication1.LogTest2 (line:207) - l4n2 cls and an error.
2013-06-10 18:41:41,836 [9] FATAL ConsoleApplication1.LogTest2 (line:208) - l4n2 cls and a fatal .


**consoleapplication1.testcls20130610.log**
2013-06-10 18:41:35,767 [9] INFO  ConsoleApplication1.testCls (line:176) - ... and an Info log.
2013-06-10 18:41:38,792 [9] WARN  ConsoleApplication1.testCls (line:178) - ... and a warning 1.
2013-06-10 18:41:38,795 [9] WARN  ConsoleApplication1.testCls (line:180) - ... and a warning 2.
2013-06-10 18:41:38,800 [9] WARN  ConsoleApplication1.testCls (line:181) - ... and a warning 3.
2013-06-10 18:41:38,804 [9] WARN  ConsoleApplication1.testCls (line:183) - ... and a warning 4.
2013-06-10 18:41:38,806 [9] WARN  ConsoleApplication1.testCls (line:184) - ... and a warning 5.
2013-06-10 18:41:41,809 [9] WARN  ConsoleApplication1.testCls (line:187) - ... and a warning 6.
2013-06-10 18:41:41,813 [9] WARN  ConsoleApplication1.testCls (line:189) - ... and a warning 7.
2013-06-10 18:41:41,817 [9] FATAL ConsoleApplication1.testCls (line:192) - ... and a fatal .
2013-06-10 18:41:41,823 [9] WARN  ConsoleApplication1.testCls (line:195) - ... and a warning 8.
2013-06-10 18:41:41,826 [9] ERROR ConsoleApplication1.testCls (line:196) - ... and an error.
2013-06-10 18:41:41,828 [9] FATAL ConsoleApplication1.testCls (line:199) - ... and a fatal .

一切都好。但是....如果我在程序运行期间更改配置文件,假设我将级别从 INFO 更改为 FATAL

            <logger name="ConsoleApplication1.testCls">
            <level value="FATAL" />  <!-- INFO to FATAL -->
        </logger>

现在的两个日志文件是:

 **consoleapplication1.logtest220130610.log**
2013-06-10 19:05:20,880 [9] WARN  ConsoleApplication1.LogTest2 (line:182) - l4n2 cls and a warning -1.
**consoleapplication1.testcls20130610.log**
2013-06-10 19:05:17,848 [9] INFO  ConsoleApplication1.testCls (line:176) - ... and an Info log.
2013-06-10 19:05:20,872 [9] WARN  ConsoleApplication1.testCls (line:178) - ... and a warning 1.
2013-06-10 19:05:20,875 [9] WARN  ConsoleApplication1.testCls (line:180) - ... and a warning 2.
2013-06-10 19:05:20,877 [9] WARN  ConsoleApplication1.testCls (line:181) - ... and a warning 3.
2013-06-10 19:05:20,884 [9] WARN  ConsoleApplication1.testCls (line:183) - ... and a warning 4.
2013-06-10 19:05:20,886 [9] WARN  ConsoleApplication1.testCls (line:184) - ... and a warning 5.

我想知道为什么监视功能无法正确更新日志并且即使我只更改一个记录器数据似乎也会影响其他日志文件?我可以知道原因吗?我的代码有什么问题

4

1 回答 1

0

当您更改配置文件时,您的程序将运行。Log4net 将重新读取您的配置文件,因为Watch:=True. 但是,您的 genLogXMLCfg 未执行。因此,在您保存新配置后,您的附加程序配置未配置。没有附加程序,因此您在日志中看不到任何新条目。

于 2013-06-11T07:58:01.170 回答