You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Khoi Nguyen Minh <Kh...@FPT.COM.VN> on 2008/04/17 17:57:16 UTC

Problem with File name in RollingFileAppender

Hi everyone, 

I'm newbie in Log4Net. I have studied Log4Net in few days and I have a problem with it.

I'm working on multi-portal framework, which store many sites. Each site has an ID (SiteId), I want logging each site in a folder named = ID of this site. For example, ManageUser.log for Site with siteid=123 will be stored in folder Portal_Logs\123.

 

|- Potal_Logs\

|---\123 for site with siteid=123

|---\456 for site with siteid=456

 

I've tried to use GlobalContext properties, but it doesn't work. In the log file the SiteId Property is correct, but in the File name, the SiteId is null.

 

 

-----------------------------------------

Here is the code in Global.asax.vb

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

log4net.Config.XmlConfigurator.ConfigureAndWatch(New System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase & "Log4NET.config"))

log4net.GlobalContext.Properties("SiteId") = New Portal.Log.Util.SiteIdProvider

End Sub

--------------------------------------------- 

Here is the code in Portal.Log.Util.SiteIdProvider

Imports System.Web

Namespace Portal.Log.Util

Public Class SiteIdProvider

Public Overrides Function ToString() As String

If Not HttpContext.Current.Session Is Nothing Then

Return HttpContext.Current.Session("SiteId")

Else

Return "-1"

End If

End Function

End Class

End Namespace

-----------------------------------------------------

Here is my log4net.config file

 

<log4net>

  <appender name="ManageUserRollingFile" type="log4net.Appender.RollingFileAppender,log4net">

    <file type="log4net.Util.PatternString"  value="C:\\Potal_Logs\\%property{SiteId}\\ManageUser"/>

    <param name="Encoding" value="utf-8"/>

    <param name="AppendToFile" value="true"/>

    <param name="RollingStyle" value="Composite"/>

    <param name="DatePattern" value=".yyyy.MM.dd.'log'"/>

    <param name="MaxSizeRollBackups" value="10"/>

    <param name="MaximumFileSize" value="10KB"/>

    <param name="StaticLogFileName" value="false"/>

    <lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" />

    <layout type="log4net.Layout.PatternLayout,log4net">

      <header value="[BEGIN LOGGING AT %date]=======================================%newline" type="log4net.Util.PatternString" />

      <footer value="[END   LOGGING AT %date]=======================================%newline" type="log4net.Util.PatternString" />

      <param name="ConversionPattern" value="%date [%-5thread] %-5level %logger [%property{SiteId}] - %property{Username} %-15property{UserHostAddress} %property{Url} - %message%newline %exception"/>

    </layout>

  </appender>

  

  <logger name="Portal.Modules.ManageUser">

    <level value="ALL" />

    <appender-ref ref="ManageUserRollingFile" />

  </logger>

</log4net>

 

Thanks so much!

--------------------------- 

Nguyen Minh Khoi| FPT  Information System Ltd.,| FPT  Information System Service Sector