Hi
One of the basic log4net things is the named logger. If you are using the same loggername, you will allways work on the same instance of the logger. If you configured a named logger once, you do not need to do this again. The repository stores the reference to the once configured logger.
I guess you thought about a workflow like this: - get the MESSAGE logger - configure the logger to use a custom logfile - log messages - forget the logger instance, including configuration
this is bad, very bad softwaredesign and yes, it would affect other threads that use the same logger.
what about this: use two explicit loggers for every location (one for messages, one for calculation). Lets say you have following 3 locations: Location1, Location2, Location3 the used logger could be: MESSAGE.Location1, MESSAGE.Location2, MESSAGE.Location3 CALCULATION.Location1, CALCULATION.Location2, CALCULATION.Location3
the new logger inherits level and appender from MESSAGE and CALCULATION like configured in configfile. So every Message goes to the main configfiles. Additional you add a custom fileappender by code to the child loggers, so you have one logfile per Location and one global logfile.
see this simple custom Logmanager Class, providing the new members: GetMessageLogger and GetCalculationLogger. They take the location as parameter and return the configured logger (ILog to be precise).
Public Class LogManager Private Const CalculatiosLoggerRoot As String = "calculations" Private Const MessagesLoggerNameRoot As String = "messages" Private Const CalculationsLogDirectory As String = "c:\tmp" Private Const MessageLogDirectory As String = "c:\tmp"
Private Sub New() End Sub
Friend Shared Function GetLogger(ByVal Name As String) As log4net.ILog Return log4net.LogManager.GetLogger(Name) End Function
Friend Shared Function GetMessageLogger(ByVal Location As String) As log4net.ILog Dim back = GetLogger(String.Format("{0}.{1}", MessagesLoggerNameRoot, Location)) AddAppender(back, MessageLogDirectory, String.Format("{0}.log", Location)) Return back End Function
Friend Shared Function GetCalculationLogger(ByVal Location As String) As log4net.ILog Dim back = GetLogger(String.Format("{0}.{1}", CalculatiosLoggerRoot, Location)) AddAppender(back, CalculationsLogDirectory, String.Format("{0}.log", Location)) Return back End Function
Private Shared Sub AddAppender(ByVal Logger As log4net.ILog, ByVal Directory As String, ByVal LogFileName As String) Dim hierarchieLogger = TryCast(Logger.Logger, log4net.Repository.Hierarchy.Logger) If Not hierarchieLogger Is Nothing Then If hierarchieLogger.Appenders.Count = 0 Then Dim newAppender = New log4net.Appender.FileAppender() newAppender.Layout = New log4net.Layout.SimpleLayout() newAppender.File = System.IO.Path.Combine(Directory, LogFileName) hierarchieLogger.AddAppender(hierarchieLogger) End If End If End Sub End Class
|