using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace ServiceStack.Logging.EntLib5
{
/// Wrapper over the Enterprise Library 5.0 Logging Application Block
public class EntLib5Logger : ILog
{
private readonly LogWriter logWriter;
///
/// Initializes a new instance of the class.
///
public EntLib5Logger()
{
var factory = new EntLib5Factory();
logWriter = factory.CreateDefault();
}
///
/// Initializes a new instance of the class.
///
/// The type name.
public EntLib5Logger(string typeName)
{
var factory = new EntLib5Factory();
logWriter = factory.CreateDefault();
logWriter.SetContextItem("type", typeName);
}
///
/// Initializes a new instance of the class.
///
/// The type.
public EntLib5Logger(Type type)
{
var factory = new EntLib5Factory();
logWriter = factory.CreateDefault();
logWriter.SetContextItem("type", type.Name);
}
///
/// Specifies whether the Debug listener this functional
///
public bool IsDebugEnabled
{
get
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
#region DEBUG Handlers
///
/// Logs a Debug message.
///
/// The message.
public void Debug(object message)
{
if (IsDebugEnabled)
{
System.Diagnostics.Debug.WriteLine(message);
}
else
{
if (logWriter.IsLoggingEnabled())
WriteMessage(message, TraceEventType.Verbose, null, null);
}
}
///
/// Logs a Debug message and category.
///
/// The message.
/// The category.
public void Debug(object message, string category)
{
if (IsDebugEnabled)
{
System.Diagnostics.Debug.WriteLine(message, category);
}
else
{
if (logWriter.IsLoggingEnabled())
WriteMessage(message, TraceEventType.Verbose, category, null);
}
}
///
/// Logs a Debug message and exception.
///
/// The message.
/// The exception.
public void Debug(object message, Exception exception)
{
if (IsDebugEnabled)
{
System.Diagnostics.Debug.WriteLine(message);
}
else
{
if (logWriter.IsLoggingEnabled())
WriteMessage(message, TraceEventType.Verbose, null, exception);
}
}
///
/// Logs a Debug message, category and exception.
///
/// The message.
/// The category.
/// The exception.
public void Debug(object message, string category, Exception exception)
{
if (IsDebugEnabled)
{
System.Diagnostics.Debug.WriteLine(message, category);
}
else
{
if (logWriter.IsLoggingEnabled())
WriteMessage(message, TraceEventType.Verbose, category, exception);
}
}
///
/// Logs a Debug format message.
///
/// The format.
/// The args.
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
{
System.Diagnostics.Debug.Print(format, args);
}
else
{
throw new NotSupportedException("The DebugFormat method is not supported. Modify the Log Message Format using a formatter in the loggingConfiguration section.");
}
}
#endregion
#region ERROR Handlers
///
/// Logs an Error message.
///
/// The message.
public void Error(object message)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Error, null, null);
}
}
///
/// Logs an Error message and category.
///
/// The message.
/// The category.
public void Error(object message, string category)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Error, category, null);
}
}
///
/// Logs an Error message and exception.
///
/// The message.
/// The exception.
public void Error(object message, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Error, null, exception);
}
}
///
/// Logs an Error message, category and exception.
///
/// The message.
/// The category.
/// The exception.
public void Error(object message, string category, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Error, category, exception);
}
}
///
/// Logs a Error format message.
///
/// The format.
/// The args.
public void ErrorFormat(string format, params object[] args)
{
if (logWriter.IsLoggingEnabled())
{
throw new NotSupportedException("The ErrorFormat method is not supported. Modify the Log Message Format using a formatter in the loggingConfiguration section.");
}
}
#endregion
#region FATAL Handlers
///
/// Logs a Fatal Error message.
///
/// The message.
public void Fatal(object message)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Critical, null, null);
}
}
///
/// Logs a Fatal Error message and category.
///
/// The message.
/// The category.
public void Fatal(object message, string category)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Critical, category, null);
}
}
///
/// Logs a Fatal Error message and exception.
///
/// The message.
/// The exception.
public void Fatal(object message, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Critical, null, exception);
}
}
///
/// Logs a Fatal Error message, category and exception.
///
/// The message.
/// The category.
/// The exception.
public void Fatal(object message, string category, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Critical, category, exception);
}
}
///
/// Logs a Error format message.
///
/// The format.
/// The args.
public void FatalFormat(string format, params object[] args)
{
if (logWriter.IsLoggingEnabled())
{
throw new NotSupportedException("The FatalFormat method is not supported. Modify the Log Message Format using a formatter in the loggingConfiguration section.");
}
}
#endregion
#region INFO Handlers
///
/// Logs an Info message.
///
/// The message.
public void Info(object message)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Information, null, null);
}
}
///
/// Logs an Info message and category.
///
/// The message.
/// The category.
public void Info(object message, string category)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Information, category, null);
}
}
///
/// Logs an Info message and exception.
///
/// The message.
/// The exception.
public void Info(object message, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Information, null, exception);
}
}
///
/// Logs an Info message, category and exception.
///
/// The message.
/// The category.
/// The exception.
public void Info(object message, string category, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Information, category, exception);
}
}
///
/// Logs an Info format message.
///
/// The format.
/// The args.
public void InfoFormat(string format, params object[] args)
{
if (logWriter.IsLoggingEnabled())
{
throw new NotSupportedException("The InfoFormat method is not supported. Modify the Log Message Format using a formatter in the loggingConfiguration section.");
}
}
#endregion
#region WARN Handlers
///
/// Logs a Warning message.
///
/// The message.
public void Warn(object message)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Warning, null, null);
}
}
///
/// Logs a Warning message and category.
///
/// The message.
/// The category.
public void Warn(object message, string category)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Warning, category, null);
}
}
///
/// Logs a Warning message and exception.
///
/// The message.
/// The exception.
public void Warn(object message, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Warning, null, exception);
}
}
///
/// Logs a Warning message, category and exception.
///
/// The message.
/// The category.
/// The exception.
public void Warn(object message, string category, Exception exception)
{
if (logWriter.IsLoggingEnabled())
{
WriteMessage(message, TraceEventType.Warning, category, exception);
}
}
///
/// Logs a Warning format message.
///
/// The format.
/// The args.
public void WarnFormat(string format, params object[] args)
{
if (logWriter.IsLoggingEnabled())
{
throw new NotSupportedException("The WarnFormat method is not supported. Modify the Log Message Format using a formatter in the loggingConfiguration section.");
}
}
#endregion
private void WriteMessage(object message, TraceEventType eventType, object category = null, Exception exception = null)
{
var entry = new LogEntry();
entry.Severity = eventType;
//Add Message
if (message != null)
entry.Message = message.ToString();
//Add category
if (category != null)
entry.Categories.Add(category.ToString());
//Add exception, user name, detailed date & time if needed
entry.ExtendedProperties.Add("Exception", exception);
entry.TimeStamp = DateTime.UtcNow;
logWriter.Write(entry);
}
}
}