logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nicko Cadell" <ni...@neoworks.com>
Subject RE: Including datetime stamp in the log messages.
Date Mon, 19 Sep 2005 16:24:42 GMT
How do you want to identify the different log files. I guess it is going
to be a combination of action type and some form of unique key.
It is no good using the thread ID as the key as threads are reused and
you have no control over that at all. 

If you really want to write to separate files for each action then you
should probably have a look at the PatternFileAppender:

http://tinyurl.com/7unou
http://cvs.apache.org/viewcvs.cgi/logging-log4net/examples/net/1.0/Appen
ders/SampleAppendersApp/cs/src/Appender/

This is not shipped as part of the log4net assembly but is a sample file
appender that allows the file property to be derived from the logging
event.

You could configure this appender using log4net's XML configuration as
follows:

<appender name="PatternFileAppender"
type="SampleAppendersApp.Appender.PatternFileAppender,
SampleAppendersApp">

  <file value="log-%property{action}-%property{id}.log" />

  <layout type="log4net.Layout.PatternLayout"
value="%date[%thread]%-5level %logger - %message%newline" />

</appender>


This configuration tells the PatternFileAppender to build up the file
name using the ThreadContextProperties 'action' and 'id'.

In your code you set these properties like this:

log4net.ThreadContext.Properties["action"] = "submit";
log4net.ThreadContext.Properties["id"] = uniqueIdForRequest;

You need to set these properties on the thread that is performing the
action you want to log to the file, and you need to set them before any
of the logging relating to that action. It is probably best to set the
properties to null after the action has finished so that they cannot
leak unexpectedly when the runtime reuses your thread.

In this way you just create and configure 1 appender and it decided
which file to use for each message as it arrives. This should be a lot
simpler than creating an appender per action.


Nicko

> -----Original Message-----
> From: Ramaa Davanagere [mailto:RDAVANAG@mobius.com] 
> Sent: 19 September 2005 17:09
> To: 'Log4NET User'
> Subject: RE: Including datetime stamp in the log messages.
> 
> 
> Well, what I'm trying to accomplish is, to log each 
> transaction request to a separate log file. Since our 
> customer support department is going to looking at these log 
> files, it will be easier if each of the transaction is in a 
> separate log file.
> 
> With our software, we can do content submission and content 
> management. So if the user submits a piece of content, I want 
> to save the log messages pertained to this submit (from start 
> to end) in one log file. 
> 
> If a user changes the content status (or something else) via 
> content management, I want to save the entire process in a 
> different log file.
> 
> How can I accomplish this? 
> 
> 
> -----Original Message-----
> From: Nicko Cadell [mailto:nicko@neoworks.com]
> Sent: Monday, September 19, 2005 11:32 AM
> To: Log4NET User
> Subject: RE: Including datetime stamp in the log messages. 
> 
> The %thread pattern is not evaluated until a message is 
> logged, then it is evaluated on the thread that is logging. 
> It is evaluated separately for each message logged as each 
> message may be logged from a different thread. This may not 
> be the same thread that is used to configure logging, 
> therefore the values would be different. 
> 
> If you want to get the ID of the current thread 
> programmatically you can
> use: 
> 
> AppDomain.GetCurrentThreadId(); 
> 
> I don't know what you will gain by using the thread id in the 
> file name, unless you are configuring a separate FileAppender 
> per thread, which you probably shouldn't be doing. 
> 
> Nicko 
> 
> > -----Original Message-----
> > From: Ramaa Davanagere [mailto:RDAVANAG@mobius.com]
> > Sent: 19 September 2005 16:23
> > To: 'Log4NET User' 
> > Subject: RE: Including datetime stamp in the log messages. 
> > 
> > 
> > Is there a way to read the [%thread] value after its being 
> > set? What I mean is, after this line is called, 
> > 
> > oPatternLayout.ConversionPattern = "%date[%thread] %-5level 
> > %message%newline"; oPatternLayout.ActivateOptions(); 
> > 
> > Is it possible to do something like this? 
> > 
> > String sThreadID = oPatterLayout.Property["thread"]; 
> > 
> > And then use the threadID in my filename. 
> > 
> > String sFileName = "\myLatestLog_" + sThreadID + ".txt" 
> > oFileAppender.File = @sRootPath + @FileName; 
> > 
> > Can I do something like this? 
> > 
> > -----Original Message----- 
> > From: Nicko Cadell [mailto:nicko@neoworks.com] 
> > Sent: Monday, September 19, 2005 11:11 AM 
> > To: Log4NET User 
> > Subject: RE: Including datetime stamp in the log messages. 
> > 
> > 
> > > Thank you! Thank you! Thank you! Thank you! 
> > > 
> > > It works. But can you tell me why it needs to be done this 
> > way? Just 
> > > curious 
> > 
> > http://logging.apache.org/log4net/release/sdk/log4net.Appender 
> > .FileAppen 
> > der.ActivateOptions.html 
> > 
> > 
> > 
> > 
> > > 
> > > -----Original Message----- 
> > > From: Nicko Cadell [mailto:nicko@neoworks.com] 
> > > Sent: Monday, September 19, 2005 10:36 AM 
> > > To: Log4NET User 
> > > Subject: RE: Including datetime stamp in the log messages. 
> > > 
> > > Try this: 
> > > 
> > > 
> > > log4net.Appender.FileAppender oFileAppender = new 
> > > log4net.Appender.FileAppender(); 
> > > 
> > > log4net.Layout.PatternLayout oPatternLayout = new 
> > > log4net.Layout.PatternLayout(); 
> > > 
> > > oPatternLayout.Header = "[Begin]\r\n"; 
> > > oPatternLayout.Footer = "[End]\r\n\r\n"; 
> > > oPatternLayout.ConversionPattern = "%date[%thread] %-5level 
> > > %message%newline"; oPatternLayout.ActivateOptions(); 
> > > 
> > > oFileAppender.Layout = oPatternLayout; 
> > > 
> > > oFileAppender.File = .... 
> > > oFileAppender.ActivateOptions(); 
> > > 
> > > Then configure log4net to use the oFileAppender. 
> > > 
> > > 
> > > Cheers, 
> > > Nicko 
> > > 
> > > > -----Original Message----- 
> > > > From: Ramaa Davanagere [mailto:RDAVANAG@mobius.com] 
> > > > Sent: 19 September 2005 14:26 
> > > > To: 'Log4NET User' 
> > > > Subject: RE: Including datetime stamp in the log messages. 
> > > > 
> > > > I'm using log4net 1.2.9.0 
> > > > 
> > > >  
> > > > 
> > > > Right now, my Fileappender output looks like this 
> > > > 
> > > >  
> > > > 
> > > > Void 
> > > > WriteToLog_Info(System.String):busContent.clsSaveContent.Save( 
> > > > ): After calling CreateContentCache() 
> > > > 
> > > > Void 
> > > > WriteToLog_Info(System.String):busContent.clsSaveContent.Save( 
> > > > ) - Leaving 
> > > > 
> > > > Void 
> > > > WriteToLog_Info(System.String):busContent.clsRetrieveContent.R 
> > > > etrieve() - Entering 
> > > > 
> > > >  
> > > > 
> > > > But I want it to look more like this... 
> > > > 
> > > >  
> > > > 
> > > > 2005-08-30 08:56:27,968[120048]INFO 
> > > > busContent.clsSaveContent.Save(): After calling 
> > CreateContentCache() 
> > > > 
> > > > 2005-08-30 08:56:27,968[120049]INFO 
> > > > busContent.clsSaveContent.Save(): Leaving 
> > > > 
> > > > 2005-08-30 08:56:27,968[120050]INFO 
> > > > busContent.clsRetrieveContent.Retrieve() - Entering 
> > > > 
> > > > 2005-08-30 08:57:58,890[129088]ERROR 
> > > > busContent.clsRetrieveContent.SetVersionNumber(): Object 
> > > variable not 
> > > > set 
> > > > 
> > > >  
> > > > 
> > > > 120049 should be the thread id. 
> > > > 
> > > >  
> > > > 
> > > > clsSaveContent and clsRetrieveContent are 2 classes of 
> busContent 
> > > > component. 
> > > > 
> > > >  
> > > > 
> > > > I'm more interested in getting the initial part (2005-08-30 
> > > > 08:56:27,968[120048]INFO) of the message. 
> > > > 
> > > >  
> > > > 
> > > >  
> > > > 
> > > > -----Original Message----- 
> > > > From: Nicko Cadell [mailto:nicko@neoworks.com] 
> > > > Sent: Monday, September 19, 2005 9:04 AM 
> > > > To: Log4NET User 
> > > > Subject: RE: Including datetime stamp in the log messages. 
> > > > 
> > > >  
> > > > 
> > > > Which version of log4net are you using? 
> > > > 
> > > >  
> > > > 
> > > > What does the output of your FileAppender look like at 
> > the moment? 
> > > > 
> > > >  
> > > > 
> > > > In your desired output example you have a chunk in square 
> > > > brackets (e.g. 
> > > > 
> > > > [120048]) between the date and the level name, what do you 
> > > want here? 
> > > > 
> > > > Thread ID? 
> > > > 
> > > >  
> > > > 
> > > > From your example is "Void WriteToLog_Info(System.String)" 
> > > > part of your 
> > > > 
> > > > log message text? 
> > > > 
> > > >  
> > > > 
> > > >  
> > > > 
> > > > Cheers, 
> > > > 
> > > > Nicko 
> > > > 
> > > >  
> > > > 
> > > > > -----Original Message----- 
> > > > 
> > > > > From: Ramaa Davanagere [mailto:RDAVANAG@mobius.com] 
> > > > 
> > > > > Sent: 19 September 2005 13:56 
> > > > 
> > > > > To: 'log4net-user@logging.apache.org' 
> > > > 
> > > > > Subject: Including datetime stamp in the log messages. 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > I'm using a fileappender and logging messages to a text file. 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > My Fileappender and PatternLayout are set like this... 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > log4net.Appender.FileAppender oFileAppender = new 
> > > > 
> > > > > log4net.Appender.FileAppender(); 
> > > > 
> > > > > 
> > > > 
> > > > > log4net.Layout.PatternLayout oPatternLayout = new 
> > > > 
> > > > > log4net.Layout.PatternLayout(); 
> > > > 
> > > > > 
> > > > 
> > > > > oPatternLayout.Header = "[Begin]\r\n"; 
> > > > 
> > > > > 
> > > > 
> > > > > oPatternLayout.Footer = "[End]\r\n\r\n"; 
> > > > 
> > > > > 
> > > > 
> > > > > oPatternLayout.ConversionPattern = "%d %-5p %c %m%n"; 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > But I want the output to look like the sample shown below. I 
> > > > 
> > > > > want to include the datetime stamp for each message that is 
> > > > 
> > > > > logged to the text file followed by the message type 
> > > > 
> > > > > constants (debug, error, fatal, info, etc) and then my 
> > > log message. 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > 2005-08-30 08:56:27,968[120048]INFO 
> > > > 
> > > > > MyCompanyName.MyProductName.ErrorHandler - Void 
> > > > 
> > > > > WriteToLog_Info(System.String) about to write a log message 
> > > > 
> > > > > 
> > > > 
> > > > > 2005-08-30 08:57:58,890[129088]ERROR 
> > > > 
> > > > > MyCompanyName.MyProductName.ErrorHandler - Void 
> > > > 
> > > > > WriteToLog_Err(System.String): Object variable not set 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > Can somebody look at my conversionPattern string and let me 
> > > > 
> > > > > know what is wrong? 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > Thanks. 
> > > > 
> > > > > 
> > > > 
> > > > >  
> > > > 
> > > > > 
> > > > 
> > > > > 
> > > > 
> > > > 
> > > 
> > > 
> > 
> > 
> 
> 

Mime
View raw message