Configuring EL Logging by code (add trace listener)

Topics: Logging Application Block
Nov 21, 2007 at 10:34 AM
I'm trying to configure EL Logging by code because I need create new trace listenters dynamically in run time, I have wrote this code but it doesn't run... Please, where is the problem? Thanks!

TextFormatter textFormatter =
new TextFormatter("Dynamic Format, {timestamp}, {severity}, {category}, {title}, {message}");

RollingFlatFileTraceListener rollingFlatFileTraceListener =
new RollingFlatFileTraceListener("C:\\" + "Dynamic" + ".log", "", "", textFormatter, 500,
"yyyy-MM-dd", RollFileExistsBehavior.Increment, RollInterval.Day);

LogSource logSource = new LogSource("DynamicGeneral" , System.Diagnostics.SourceLevels.All);

//the file is not created
Logger.Write("Message", "DynamicGeneral" , 1, 1, System.Diagnostics.TraceEventType.Information, "Tittle");
Nov 21, 2007 at 1:58 PM
Here is a tutorial I wrote quite some time ago using EntLib 2.0, but it should apply for EntLib 3.1. It shows how to programmatically use the classes associated with the Logging Application Block:

Enterprise Library 2.0 Logging Application Block Part III - Programmatically Using Logging Application Block With No Configuration File




David Hayden
Microsoft MVP C#
Nov 21, 2007 at 3:29 PM
Hi David,

Thank you for your answer, I have already read your tutorial but I would like use the class Logger.Write() instead of create your class MyLogger.Write().
Is it possible??
Nov 26, 2007 at 2:11 PM

I have not been able to get a FormattedDatabaseTraceListener to work. I can write the event to a database if I call the stored procedure "WriteLog" directly, but not by using the trace listener. I have seen examples of a custom trace listener, but basically all they were is providing a different interface to calling the stored procedure. My code is below. Please help if you can, thank you.

--------------------------- My code -----------------------------------------

TextFormatter formatter = new TextFormatter("Timestamp: {timestamp}{newline}" +
"Message: {message}{newline}" +
"Category: {category}{newline}" );

LogSource emptyTraceSource = new LogSource("none");
LogSource errorsTraceSource = new LogSource(ErrorCategory, System.Diagnostics.SourceLevels.All);

Data.Database db = Data.DatabaseFactory.CreateDatabase();
errorsTraceSource.Listeners.Add(new FormattedDatabaseTraceListener(db, "WriteLog", "AddCategory", formatter));

IDictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
// Add to Category "Error" our EventLog Listener with the corresponding category in it.
traceSources.Add(errorsTraceSource.Name, errorsTraceSource);

return new LogWriter(new ILogFilter0, // ICollection<ILogFilter> filters
traceSources, // IDictionary<string, LogSource> traceSources
emptyTraceSource, // LogSource allEventsTraceSource
emptyTraceSource, // LogSource notProcessedTraceSource
errorsTraceSource, // LogSource errorsTraceSource
ErrorCategory, // string defaultCategory
false, // bool tracingEnabled
true); // bool logWarningsWhenNoCategoriesMatch

LogEntry ent = new LogEntry();
ent.Message = "This is an error message created.";
ent.Severity = System.Diagnostics.TraceEventType.Error;
LogWriter writer;