tags:

views:

87

answers:

3

I am using log4net in my .NET projects and logs are being created with names like:

C:\\Inetpub\\zz_appLogs\\<hard coded sitename>\\<yyyyMMdd>\\<hard coded file name with txt extension>

Now I want to change the log file name with current site name in IIS. I have tried this in log4net settings but it didnt work:

<file type="log4net.Util.PatternString" 
      value="C:\Inetpub\zz_appLogs\MYsiteLogs\" />
...
<datePattern 
      value="yyyyMMdd\\&quot;%property{CURRENTSITENAME}_info.txt&quot;" />

The resultant logs are having filename as follows:

C:\Inetpub\zz_ErrorLogs\MVCdoctoolLogs\20100304\  
      %property{CURRENTSITENAME}_error.txt`

I am setting the property in app_start in Global.ascx file of project:

log4net.GlobalContext.Properties["CURRENTSITENAME"] = 
        System.Web.Hosting.HostingEnvironment.SiteName;

Can you tell me how I can set the sitename from IIS in the resultant txt file created by log4net.

thanks in advance :)

A: 

ok.. now i am getting the sitename in case when i put it in "file type="log4net.Util.PatternString" value="C:\Inetpub\zz_appLogs\MYsitelogs\%property{CURRENTSITENAME}_info.txt"

But how can i read the sitename from IIS in the datepattern .I am trying this but its not working: "datePattern value="yyyyMMdd\"%property{CURRENTSITENAME}_info.txt""

gbhatnagar
A: 

I tried myself and the following gave me the expected result. I did test with a console application, but I do not see how this should change anything:

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="c:\temp\%date{yyyyMMdd}\%property{SiteName}.txt" />
      <rollingStyle value="Size" />
      <appendToFile value="true" />
      <maximumFileSize value="100KB" />
      <maxSizeRollBackups value="1" />
      <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%2thread] %-5level %logger - %message%newline" />
      </layout>
  </appender>
Stefan Egli
gbhatnagar
A: 

hi, i am using this : file type="log4net.Util.PatternString" value="C:\Inetpub\zz_appLogs\MVCdoctoolLogs\%date{yyyyMMdd}\%property{CURRENTSITENAME}.txt"

But the log file are now creating at this path : C:\Inetpub\zz_appLogs\MysitaName\20100304\MYTESTSITENAME.txt.2010-03-04

i have not specified "datePattern" in appender.But it is appening the date in filename at end. Now , i can read the property for sitename & date too in folder path.Buy how about the date at end of filename...

gbhatnagar