I am using the Microsoft Enterprise Library 5.0 Logging block and I wanted to know the difference between the LogWriter and the Logger classes. I have a few custom trace listeners that I have built to be used in my logging and I wanted to know if using Logger vs. LogWriter would have any effect.
From MSDN http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logwriter.aspx
To write log messages to the default configuration, use the Logger facade. Only create an instance of a LogWriter if you need to write log messages using a custom configuration.
Example code #1:
namespace ExampleOne
{
using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
public class Program
{
private static LogWriter logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
public static void Main(string[] args)
{
try
{
throw new Exception("My test exception message");
}
catch (Exception ex)
{
LogEntry logEntry = new LogEntry();
logEntry.Message = "Error: " + ex.ToString();
logEntry.Categories.Add("General");
logEntry.Severity = TraceEventType.Error;
logger.Write(logEntry);
}
}
}
}
Example code #2:
namespace ExampleTwo
{
using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
public class Program
{
public static void Main(string[] args)
{
try
{
throw new Exception("My test exception message");
}
catch (Exception ex)
{
LogEntry logEntry = new LogEntry();
logEntry.Message = "Error: " + ex.ToString();
logEntry.Categories.Add("General");
logEntry.Severity = TraceEventType.Error;
Logger.Write(logEntry);
}
}
}
}