Wednesday, October 22, 2008

Logging using Enterprise librarary

Interface:

ILogSink Interface

public interface ILogSink
{

void SendMessage(LogEntry log);

ILogFormatter Formatter { get; set; }
}




Testing Logging Application Block:

using System;
using System.Collections;

using Microsoft.Practices.EnterpriseLibrary.Logging;

public class GeneralLogEntry : LogEntry
{
public GeneralLogEntry(string message) : this(message, 2) {}

public GeneralLogEntry(string message, int priority) : base()
{
Category = "General";
Priority = priority;
Severity = Severity.Unspecified;
Message = message;
}
}

public class ExceptionLogEntry : LogEntry
{
public ExceptionLogEntry(string message) : this(message, 5) {}

public ExceptionLogEntry(string message, int priority) : base()
{
Category = "Exception";
Priority = priority;
Severity = Severity.Error;
Message = message;
}
}

public class PerformanceLogEntry : LogEntry
{
public PerformanceLogEntry(string message) : this(message, 3) {}

public PerformanceLogEntry(string message, int priority) : base()
{
Category = "Performance";
Priority = priority;
Severity = Severity.Information;
Message = message;
}
}

public class SecurityLogEntry : LogEntry
{
public SecurityLogEntry(string message) : this(message, 5) {}

public SecurityLogEntry(string message, int priority) : base()
{
Category = "Security";
Priority = priority;
Severity = Severity.Warning;
Message = message;
}
}

public class DebugLogEntry : LogEntry
{
public DebugLogEntry(string message) : this(message, 1) {}

public DebugLogEntry(string message, int priority) : base()
{
Category = "Debug";
Priority = priority;
Severity = Severity.Information;
Message = message;
}
}

public class MyClass
{
public static void Main()
{
Logger.Write(new GeneralLogEntry("A Beautiful Day"));
Logger.Write(new DebugLogEntry("Loading Array"));
Logger.Write(new SecurityLogEntry("User is not authenticated."));
Logger.Write(new PerformanceLogEntry("Application running slow...", 4));
Logger.Write(new ExceptionLogEntry("Division by zero exception."));
}

}

No comments: