Logging

To help locate any defects that are present in the software, it's beneficial to generate log messages that tell you what the application is doing. Moreover logging could be useful for security and auditing reasons to help the system administrators to have information about accesses and operations over application. NStruts uses the Dotnet Commons Logging framework to implement logging needs.

Dotnet Common Logging Framework

The Commons Logging package is an open source Logging library that allows developers to utilize a common logging API, while maintaining the freedom to utilize many different third-party logging implementations. The Commons Logging API insulates the application and protects it from becoming coupled to a specific logging implementation.
The API provides a small set of C# classes and interfaces that an application imports and relies upon, but which has no implicit dependencies on any one logging product.
The Logging library allows developers to declaratively configure the logging implementation and the library will dynamically discover which implementation is being used. An application that utilizes the Commons Logging API does not have to be modified when the logging implementation is changed; this is the greatest benefit of such a package.

The Commons Logging package supports several different logging implementations out of the box:
  • Log4Net
  • SimpleLog (Simple text file Logger)
  • System.Diagnostics.Trace
  • Microsoft Logging and Instrumentation Application Block (LIAB).

Configuration

The NStruts sample application provide a Dotnet Commons Logging Configuration's example that use Log4Net implementation, an excerpt is outlined below.

<configSections>

    <!--
    =============================================
    Log4Net config section
    =============================================
    -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

</configSections>

<appSettings>
    <!--
    ===========================================================================
    This is the only Application Setting needed for Dotnet.Commons.Logging
    ===========================================================================
    -->
    <add key="Dotnet.Commons.Logging.LogFactory"
        value="Dotnet.Commons.Logging.Impl.Log4NetFactory,Dotnet.Commons.Logging.Log4Net" />

</appSettings>


<!--
=============================================
Log4Net specific  Configuration
=============================================
-->
<log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
	</layout>
    </appender>
    <appender name="FrameworkAppender" type="log4net.Appender.FileAppender">
        <file value="nstruts.log" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="ConsoleAppender" />
    </root>
    <logger name="NStruts">
        <level value="TRACE" />
        <appender-ref ref="FrameworkAppender" />
    </logger>
</log4net>

Last edited Feb 27, 2007 at 8:51 PM by bsorrentino, version 3

Comments

JoeBlack Feb 26, 2007 at 4:10 PM 
Hi, just to let you know that I am moving Dotnet Commons into CodePlex as well. The Commons Logging section can be found here:
http://www.codeplex.com/DotnetCommons/Wiki/View.aspx?title=Logging&referringTitle=Home