Welcome to CuttingEdge.Logging
CuttingEdge.Logging is a library that helps programmers to output log statements to a variety of output targets in .NET applications. The design is based on the .NET 2.0 Provider Model. This means the library uses the same configuration and extensibility model.
Overview
CuttingEdge.Logging is a configurable logging system and can even be plugged into .NET applications without a republish or recompile. The library is written in C# .NET 3.5 in Visual Studio 2008, but the library can also be used by .NET 2.0 applications. It can be used for several types of .NET applications, such as server and desktop applications. However, Compact Edition and Silverlight are not supported.
Comparing
CuttingEdge.Logging differentiates with other logging frameworks such as
log4net and the
Enterprise Library Logging Application Block (LAB) in it's simplicity. CuttingEdge.Conditions does not allow you to configure layouts (log4net) or formatters (LAB) to control the way events are written. Neither does it support filters (log4net and LAB) to determine which event should be logged and where to. log4net even allows you to change configuration, without an application restart (
dynamic configuration) and it's compatible with Mono, .NET CE and even SSCLI.
While those two frameworks are highly configurable and composable; they are complex to configure and use. CuttingEdge.Logging is designed with the
KISS principle (keep it small, simple) in mind. You won't have to read large manuals and when you are familiar with the .NET provider model, you'll feel right at home.
Usage
C# example
The following example shows some code snippets of what code a C# developer could write using CuttingEdge.Logging.
// Log an information event to the default provider.
Logger.Log("Operation completed successfully.");
// Log a warning to the default provider.
Logger.Log(LoggingEventType.Warning, "Server is currently under high load");
// Log an error event with source location to the default provider.
Logger.Log(LoggingEventType.Error,
"Something went wrong.", MethodBase.GetCurrentMethod());
try
{
this.DoSomeAction();
}
catch (Exception ex)
{
// Log an exception to the default provider.
Logger.Log(ex);
throw;
}
// Log an information event to the default provider.
// This is equivalent to calling 'Logger.Log'.
Logger.Provider.Log("Success!", MethodBase.GetCurrentMethod());
// Log to an alternative provider
LoggingProviderBase xmlProvider = Logger.Providers["XmlLogProvider"];
xmlProvider.Log("Success!");
// Display all configured providers
foreach (LoggingProviderBase loggingProvider in Logger.Providers)
{
Console.WriteLine(loggingProvider.Name);
}
// Logging providers can be chained. Using fallback providers.
// This way you can ensure a event is being logged, even if the
// default provider fails.
LoggingProviderBase fallbackProvider = Logger.Provider.FallbackProvider;
if (fallbackProvider != null)
{
fallbackProvider.Log(LoggingEventType.Warning, "This is a warning.");
}
VB.NET example
The following example shows some code snippets of what code a VB.NET developer could write using CuttingEdge.Logging.
' Log an information event to the default provider.
Logger.Log("Operation completed successfully.")
' Log a warning to the default provider.
Logger.Log(LoggingEventType.Warning, "Server is currently under high load")
' Log an error event with source location to the default provider.
Logger.Log(LoggingEventType.[Error], _
"Something went wrong.", MethodBase.GetCurrentMethod())
Try
DoSomeAction()
Catch ex As Exception
' Log an exception to the default provider.
Logger.Log(ex)
Throw
End Try
' Log an information event to the default provider.
' This is equivalent to calling 'Logger.Log'.
Logger.Provider.Log("Success!", MethodBase.GetCurrentMethod())
' Log to an alternative provider
Dim xmlProvider As LoggingProviderBase = Logger.Providers("XmlLogProvider")
xmlProvider.Log("Success!")
' Display all configured providers
For Each loggingProvider As LoggingProviderBase In Logger.Providers
Console.WriteLine(loggingProvider.Name)
Next
' Logging providers can be chained. Using fallback providers.
' This way you can ensure a event is being logged, even if the
' default provider fails.
Dim fallbackProvider As LoggingProviderBase = Logger.Provider.FallbackProvider
If fallbackProvider IsNot Nothing Then
fallbackProvider.Log(LoggingEventType.Warning, "This is a warning.")
End If
Basic configuration
The following example shows a the app.config of a typical application that uses CuttingEdge.Logging. This configuration in this example allows logging to the Windows event log.
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- The following section should be added to the configSections. -->
<section name="logging"
type="CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"
allowDefinition="MachineToApplication"/>
</configSections>
<!-- the logging section allows you to configure logging providers -->
<logging defaultProvider="WindowsEventLogLoggingProvider">
<providers>
<!-- The WindowsEventLogLoggingProvider writes to the event log -->
<add
name="WindowsEventLogLoggingProvider"
source="MyWebApplication"
logName="MyWebApplication"
type="CuttingEdge.Logging.WindowsEventLogLoggingProvider, CuttingEdge.Logging"
/>
</providers>
</logging>
</configuration>
You can find more configuration examples on the
configuration examples page.
More Advanced Configuration
The example below shows how to configure the Logging Provider model in a web environment to log to a SQL Server database and use the Windows event log as fallback logging store. The
AspNetSqlLoggingProvider is configured with the threshold set to 'Information', which means that Debug events will not be logged. The Windows event log logging provider has the threshold set to 'Warning', which means that also 'Information' events will not be logged. By configuring the
AspNetExceptionLoggingModule http module, the web application ensures that unhandled exceptions will get logged automatically. The configured
LoggingWebEventProvider will route web events, raised by the ASP.NET health monitoring system, to the Logging infrastructure.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="logging" type="CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"
allowDefinition="MachineToApplication" />
</configSections>
<connectionStrings>
<add name="SqlLogging"
connectionString="Data Source=.;Integrated Security=SSPI;Initial Catalog=Logging;" />
</connectionStrings>
<logging defaultProvider="AspNetSqlLoggingProvider">
<providers>
<add name="AspNetSqlLoggingProvider"
type="CuttingEdge.Logging.Web.AspNetSqlLoggingProvider, CuttingEdge.Logging"
fallbackProvider="WindowsEventLogLoggingProvider"
description="ASP.NET SQL logging provider example"
connectionStringName="SqlLogging"
threshold="Information"
initializeSchema="True"
applicationName="[MyWebApplication]"
userNameRetrievalType="Membership"
logQueryString="True"
logFormData="False" />
<add name="WindowsEventLogLoggingProvider"
threshold="Warning"
source="MyWebApplication"
logName="MyWebApplication"
type="CuttingEdge.Logging.WindowsEventLogLoggingProvider, CuttingEdge.Logging" />
</providers>
</logging>
<system.web>
<httpModules>
<!-- We're defining the AspNetExceptionLoggingModule here. -->
<add name="ExceptionLogger"
type="CuttingEdge.Logging.Web.AspNetExceptionLoggingModule, CuttingEdge.Logging"/>
</httpModules>
<healthMonitoring heartbeatInterval="0" enabled="true">
<providers>
<!-- We're configuring the web event provider here. -->
<add name="LoggingWebEventProvider"
type="CuttingEdge.Logging.Web.LoggingWebEventProvider, CuttingEdge.Logging"
loggingProvider="AspNetSqlLoggingProvider" />
</providers>
<rules>
<add name="Custom Event Provider"
eventName="All Events"
provider="LoggingWebEventProvider"
profile="Default" />
</rules>
</healthMonitoring>
</system.web>
</configuration>
You can find more configuration examples on the
configuration examples page.
Extending CuttingEdge.Logging
The following example shows how to extend CuttingEdge.Logging by defining a logger that logs to the file system.
using System;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.IO;
using CuttingEdge.Logging;
namespace MyLoggingProviders
{
public class FileLoggingProvider : LoggingProviderBase
{
private string path;
public override void Initialize(string name,
NameValueCollection config)
{
if (config == null)
{
throw new ArgumentNullException("config");
}
// Call base initialize first.
base.Initialize(name, config);
// Performing provider specific initialization.
this.InitializePath(name, config);
// Always call this method last.
this.CheckForUnrecognizedAttributes(name, config);
}
protected override object LogInternal(LogEntry entry)
{
DateTime now = DateTime.Now;
string line = String.Format(
"{0} {1}\t{2}\t{3}\t{4}\n",
now.ToShortDateString(),
now.ToShortTimeString(),
entry.Severity,
entry.Message,
entry.Exception);
File.AppendAllText(this.path, line);
return null;
}
private void InitializePath(string name,
NameValueCollection config)
{
this.path = config["path"];
if (String.IsNullOrEmpty(this.path))
{
throw new ProviderException(string.Format(
"Empty or missing 'path' attribute in " +
"provider '{0}' in config file.", name));
}
config.Remove("path");
}
}
}
The following example shows the configuration for the logging provider that we've defined above:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="logging"
type="CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging"
allowDefinition="MachineToApplication"/>
</configSections>
<logging defaultProvider="FileLoggingProvider">
<providers>
<add name="FileLoggingProvider"
path="c:\mylog.txt"
type="MyLoggingProvider.FileLoggingProvider, MyLoggingProvider"
/>
</providers>
</logging>
</configuration>
More information
For more information about CuttingEdge.Logging, please visit the following links:
For more information about the ASP.NET provider model, please visit the following links:
The project is currently under development and we do like your feedback!
Happy logging!