1

I have log file location in web.config as follows.

<appSettings>
    <add key="LogPath" value="D:\Service\"/>
</appSettings>

I have log4net config in web.config as follows

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="%property{LogName}"/>
        <appendToFile value="true"/>
        <rollingStyle value="Size"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="10MB"/>
        <staticLogFileName value="true"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%m%n"/>
        </layout>
    </appender>
    <root>
        <level value="DEBUG"/>
        <appender-ref ref="RollingLogFileAppender"/>
    </root>
</log4net>

Initialized GetLogger at the top of the Service class as follows

Private Shared ReadOnly log As ILog = LogManager.GetLogger(GetType(Service))

I have created GlobalContext property for LogName as follows

log4net.GlobalContext.Properties("LogName") = ConfigurationManager.AppSettings("LogPath") & "Service." & Format(Now, "ddMMyyyy") & ".txt"

Logging is done with log.Info as follows

log.Info(strComments)

In this scenario, the log file is created as "null" under Project folder and logging the details.

I want the log location to be "D:\Service\Service.30082012.txt" according to the code above.

What am I missing? Please help me out.

CPK_2011
  • 872
  • 3
  • 21
  • 57

2 Answers2

1

Typically I separate the log4net configuration from my web.config to avoid web application restarts ( How to prevent an ASP.NET application restarting when the web.config is modified? ) when you change the web.config file.

I usually just have a log4net.config file to store all of these settings and I configure it to watch the config file for changes:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

Also I'm not quite sure why you are storing your log file directory outside of the log4net configuration unless it's just easier for you to modify. I typically keep the path in the log4net config like so:

<appender name="File" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="${ALLUSERSPROFILE}/CompanyName/Logs/ApplicationName/%date{MM-dd-yyyy} - Whatever.log" />
Community
  • 1
  • 1
Cole W
  • 15,123
  • 6
  • 51
  • 85
0

Make sure you don't have any log commands before you set the LogName property.

Also check that logname is being set correctly ie, check that

 ConfigurationManager.AppSettings("LogPath") & "Service." & Format(Now, "ddMMyyyy") & ".txt" 

is not evalulating to null

sgmoore
  • 15,694
  • 5
  • 43
  • 67