Monday, May 21, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Add new log programmatically and remove when finished
Prev Next
You are not authorized to post a reply.

Author Messages
David Gerler

04/19/2009 4:51 AM  

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/%3c2AD7ECA75635F84A87792C0B2F8692487DDC03@EXCH3.ads.bruker.de%3e

 

Thanks for your help.

Dave

David Gerler

04/19/2009 8:49 PM  
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?
 

 
Daniel Marohn

04/20/2009 10:09 AM  

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

David Gerler

04/23/2009 7:48 PM  
Actually, I was thinking more of
 
1. check for existing logger ( message.loc1)
   If (!exists)
     create new logger
2. add appender
3 log messages
4 destroy appender
5 release logger.
 
I'm not sure why that is such bad design. (I only have a few years programming and self taught).
 
So, given that that is bad design, what would my config file look like.
 
Dave
You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Add new log programmatically and remove when finished



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement