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



On Sun, Apr 19, 2009 at 8:49 PM, David Gerler <dger...@gmail.com> wrote:
> As I am trying to work on this project, I am realizing I need to provide
> more info.
>
> The project is a web service that recieves data from 120
> locations simultaneously. The problem I have is that all the data gets
> lumped into 3 primary logfiles. There are 10 threads handling data from
> these various locations.
> They are:
> root (of coarse)
> calculations.log
> messages.log
>
> Where I would like to go is to have a set of calculations.log and
> messages.log for each location so it's easier to find the logged info that
> affects a particular location when an error occurs.
>
> I am trying to avoid having a configuration file filled with loggers for
> each of the 120 locations with appenders that have different filenames.
>
> My thought was to create a new logger each time it comes into the
> calculation section and destroy it on exit. From the code I referenced in my
> first email, I can add appenders but I'm not sure about how to create a new
> logger each time without affecting the other threads.
>
> Hopefully, I'm using the correct terminology. When I say logger, I mean the
> equivalent of:
>
> <
>
> logger name="MESSAGE" additivity="true">
>
> <
>
> appender-ref ref="Message"/>
>
> </
>
> logger>
> Can anyone help me to divise a plan to handle this?
>
>
>
> On Sat, Apr 18, 2009 at 9:51 PM, David Gerler <dger...@gmail.com> wrote:
>>
>> I have an application that receives data from many locations.
>>
>> I have log4net writing to several logs already and I want that to
>> continue, but I also need to separate some of the logging out by the
>> location the data comes from.
>>
>>
>>
>> What's the best way to do it? I found this message in the archive, but I'm
>> not sure how this will affect the other logging.
>>
>>
>> http://mail-archives.apache.org/mod_mbox/logging-log4net-user/200805.mbox/%3c2ad7eca75635f84a87792c0b2f8692487dd...@exch3.ads.bruker.de%3e
>>
>>
>>
>> Thanks for your help.
>>
>> Dave
>



-- 
Daniel Marohn - mar...@sipgate.de
Telefon: +49 (0)211-63 55 55-0
Telefax: +49 (0)211-63 55 55-22
sipgate GmbH - Gladbacher Str. 74 - 40219 Düsseldorf
HRB Düsseldorf 39841 - Geschäftsführer: Thilo Salmon, Tim Mois
Steuernummer: 106/5724/7147, Umsatzsteuer-ID: DE219349391
www.sipgate.de - www.sipgate.at - www.sipgate.co.uk

Reply via email to