Logging using application blocks- Change the file name @ run time

K

kavitha

Hi,
I am using Enterprise library for trace logging into a file. I specify the
logging trace listerners in app.config. I would like to change the file name
of the trace file during run time. Can anyone pls tell me how to accomplish
the same.

I have tried adding a method that sets the trace log file name during the
run time and then logging into the file. The file name does not appear to me
during my 1st execution of the aopplication. If I run the application 2nd
time, the file path in the app.config would have got modified and the logging
takes place. I have added the app.config file and the code snippet that I use
to modify the app.config file.

App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<section name="loggingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"/>
<section name="exceptionHandling"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"/>
</configSections>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName=" "
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
name="FlatFile TraceListener" />
</listeners>
<formatters>
<add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="FlatFile TraceListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="FlatFile TraceListener" />
</listeners>
</allEvents>
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings" />
</specialSources>
</loggingConfiguration>
<exceptionHandling>
<exceptionPolicies>
<add name="LogAndThrowPolicy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="Exception">
<exceptionHandlers>
<add logCategory="General" eventId="1000" severity="Error"
title="Test Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null" priority="1"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Exception
Logging Handler"/>
</exceptionHandlers>
</add>
<add type="System.Data.SqlClient.SqlException, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="SqlException">
<exceptionHandlers>
<add logCategory="General" eventId="1000" severity="Error"
title="Test Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null" priority="1"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Exception
Logging Handler"/>
</exceptionHandlers>
</add>
<add type="System.ArgumentException, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="ArgumentException">
<exceptionHandlers>
<add logCategory="General" eventId="1000" severity="Error"
title="Test Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null" priority="1"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Exception
Logging Handler"/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>

CodeSnippet:
public void SetTraceLogPath(string <ConfigFilePath>)
{
ConfigurationFileMap objConfigPath = new ConfigurationFileMap();

objConfigPath.MachineConfigFilename = <ConfigFilePath>;

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

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

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

objFlatFileTraceListenerData.FileName = "Test.log";

entLibConfig.Save();

}


I have also gone through
http://www.codeguru.com/csharp/.net/net_framework/systemnamespace/print.php/c11281__2/
where we can add the listerener.cs file and change the file path
programatically.But if there is any other way to change the log file name
programatically, pls do let me know.

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top