How to programatically set log file name in Logging Block

Topics: Logging Application Block
Dec 15, 2008 at 5:01 PM
Hello, I am trying to get find a way to programmatically set the log file name for a flatfile trace listener or rolling flatfile listener. What I want to do is load the log file name and location from 2 registry keys when the Logger is called. Is there I way to set a filename property in LoggingSettings or something? I know the app.config is read only during runtime. Will I have to modify or extend this functionality from the Logging Block source?

I have also looked at this discussion on how to read a log file name: http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=40472
This forum post shows how to set the path for a flatfile trace listener but it is not much help to me: http://forums.asp.net/p/994264/1937749.aspx



Dec 16, 2008 at 3:17 AM
Hi,

What do you mean when the Logger is called? Do you mean that you will set the filename property when the Logger.Write is called?


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Dec 16, 2008 at 3:24 AM
Yes, sorry, I should have been more specific. I need to set the log filename when the Logger.Write method is called. I'll also be passing a LogEntry to the Logger.Write() method. Thanks in advance for your help.
Dec 16, 2008 at 3:40 AM
Edited Dec 16, 2008 at 3:48 AM
Hi,

In your case, you should modify the Logging Application Block source, and implement a logic that will edit your config at runtime and set the filename property berfore the call to the actual logging. Or you can create a helper Class that will edit the config and then call the Logger.Write(log).

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Dec 16, 2008 at 3:46 AM
Could you point me to the right location in the source code? Do you have any code snippets that could help me? I don't know where the filename is loaded in the LOB. I'm still new to the Enterprise Library. Thanks.
Dec 16, 2008 at 3:57 AM
Hi,

I've got another suggestion, you can create a helper class that will edit and set the filename property at runtime then calls the Logger.Write(log), here is the sample.

Here is the sample Helper Class:

 

public static class LogHelper

 

{

 

public static void WriteLog(LogEntry entry)

 

{

 

ConfigurationFileMap configFileMap = new ConfigurationFileMap();

 

configFileMap.MachineConfigFilename =

"PathOfYourConfig";

 

 

Configuration entLibConfig = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);

 

 

LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

 

 

FlatFileTraceListenerData data = loggingSettings.TraceListeners.Get("FlatFile TraceListener") as FlatFileTraceListenerData;

 

data.FileName =

"PathFromYourRegistry";

 

entLibConfig.Save();

 

Logger.Write(entry);

 

}

}



Here is the sample call:


 

LogEntry

log = new LogEntry();

 

log.Message =

"Logged with a Helper Class...";

 

 

LogHelper.WriteLog(log);



Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Dec 16, 2008 at 4:00 AM
I'll try that and let you know how it works out. Thanks alot!