locked
Problems in dynamic file naming with multiple appenders in log4net

    問題

  • I have 3 appenders in my config file for creating 3 different types of logs. I am using dynamic naming of file in each of the 3 appenders by setting the global context properties. In some cases, i need to set the log file name dynamically for just 1 appender. When i set the file name for just 1 appender, it creates another file named "null" with no data in addition to the actual logfile whose name has been set dynamically.

    <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender"> 
    
    
    
    <file type="log4net.Util.PatternString" value="Logs\%property{applog}" /> 
    
    
    
    . 
    
    
    
    . 
    
    
    
    . 
    
    
    
    <appender name="RollingFileAppenderV2" type="log4net.Appender.RollingFileAppender"> 
    
    
    
    <file type="log4net.Util.PatternString" value="Logs\%property{dblog}" /> 
    
    
    
    . 
    
    
    
    . 
    
    
    
    <logger name="Logger1"> 
    
    
    
     <level value="DEBUG" /> 
    
    
    
     <appender-ref ref="RollingFileAppenderV1" /> 
    
    
    
    </logger> 
    
    
    
    <logger name="Logger2"> 
    
    
    
     <level value="DEBUG" /> 
    
    
    
     <appender-ref ref="RollingFileAppenderV2" /> 
    
    
    
    </logger> 
    
    
    
    
    
    
    
    

    In the VB.NET code i set the filename as :

    log4net.GlobalContext.Properties("applog") = "file1.log" 
    
    
    
    Dim logobj as log4net.Ilog = LogManager.GetLogger("Logger1") 
    
    
    
    logobj.debug("test") 
    
    
    
    
    
    
    
    

    In this case it creates "file1.log" and also another empty file with name as "null". This happens only when i am setting either of the appenders filename at runtime.
    Any help appreciated.
    2010年10月1日 上午 06:44

所有回覆

  • You need to assign a value to the dblog property, which at the moment is null.

     

    Either assign in code. E.g.

     

     

    log4net.GlobalContextProperties("dblog") = "file2.log"
    
    
    

     

     

    or else hard-code the name of the file (better IMHO as it overwrites the old versions of the file, preventing you from cluttering your output folder. 

    So this:

     

    <file type="log4net.Util.PatternString" value="Logs\%property{dblog}" />

    becomes 

    <file type="log4net.Util.PatternString" value="Logs\log2.log" />

     

     

     

     

    2011年7月4日 下午 12:57