programmatically configure enterprise library appication logging
this is what i have so far. it appears that the database is created
fine, but i am not getting anything written to the db though when i call
Write(). thanks.
public class DBLogger
{
LogWriter writer = null;
FormattedDatabaseTraceListener logFileListener = null;
LogSource mainLogSource = null;
LogSource emptyTraceSource = null;
TextFormatter formatter = null;
string sCategory = "";
Database db = null;
public DBLogger(string sDBServerName, string sDBName, string
sUser, string sPasswd, Level level,
string sMessage, string sMiscellaneous)
{
try
{
formatter = new TextFormatter("Timestamp:
{timestamp}{newline}" +
"Message: {message}{newline}" + "Category:
{category}{newline}");
emptyTraceSource = new LogSource("none");
// example: data source=arc-tms-db2005;initial
catalog=northwind;Integrated Security=SSPI
string sConnectionString = "data source=" +
sDBServerName + "; initial catalog=" +
sDBName + ";Integrated Security=SSPI";
db = new SqlDatabase(sConnectionString);
logFileListener = new FormattedDatabaseTraceListener(db,
"WriteLog", "AddCategory", formatter);
// adding collection of TraceListeners.
// LogSource provides tracing services through a set of
TraceListeners.
switch (level)
{
case (Level.DEBUG):
mainLogSource = new LogSource("MainLogSource",
SourceLevels.All);
sCategory = "Debug";
break;
case (Level.ERROR):
mainLogSource = new LogSource("MainLogSource",
SourceLevels.Error);
sCategory = "Error";
break;
case (Level.MESSAGE):
mainLogSource = new LogSource("MainLogSource",
SourceLevels.Information);
sCategory = "Message";
break;
default:
mainLogSource = new LogSource("MainLogSource",
SourceLevels.Information);
sCategory = "Message";
break;
}
mainLogSource.Listeners.Add(logFileListener);
IDictionary<string, LogSource> traceSources = new
Dictionary<string, LogSource>();
// Add to Category "Error" our EventLog Listener with
the corresponding category in it.
traceSources.Add(mainLogSource.Name, mainLogSource);
writer = new LogWriter(new ILogFilter[0], //
ICollection<ILogFilter> filters
traceSources, // IDictionary<string, LogSource>
traceSources
emptyTraceSource, // LogSource allEventsTraceSource
emptyTraceSource, // LogSource
notProcessedTraceSource
mainLogSource, // LogSource mainLogSource
"Error", // string defaultCategory
false, // bool tracingEnabled
true); // bool logWarningsWhenNoCategoriesMatch
}
catch (Exception ex)
{
}
}
/// <summary>
/// Writes an Error to the log.
/// </summary>
/// <param name="message">Error Message</param>
public void Write(string message)
{
try
{
Write(message, sCategory);
}
catch (Exception ex)
{
}
}
/// <summary>
/// Writes a message to the log using the specified
/// category.
/// </summary>
/// <param name="message"></param>
/// <param name="category"></param>
public void Write(string message, string category)
{
try
{
LogEntry ent = new LogEntry();
ent.Categories.Add(category);
ent.Message = message;
ent.Severity =
System.Diagnostics.TraceEventType.Information;
writer.Write(ent);
}
catch (Exception ex)
{
}
}
public void CloseWriter()
{
writer.Dispose();
}
}
*** Sent via Developersdex http://www.developersdex.com ***
date: Mon, 25 Feb 2008 01:40:40 -0800
author: Michael Lam