How To Logging with Microsoft.NET

How To Logging with Microsoft.NET

CheapASPNETHostingReview.com | Best and cheap ASP.NET Hosting. Through this article, we will explain in depth logging in .NET applications. We will explain this through two steps:

  1. Introduction to System.Diagnostics (prebuilt logging namespace in .NET)
  2. Introduction to Log4net

I. System.Diagnostics Namespace

The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters, Classes like EventLog which provides functionality to write to event logs, read event log entries, and create and delete event logs and event sources on the network. TraceSource which also provides a set of methods and properties that enable applications to trace the execution of code and associate trace messages with their source. In this paragraph, we will implement a tracing/Logging library based on the TraceSource Class. This library enable users to:

  • trace different scenarios inside applications [StartStopErrorInformationCriticalWarning]
  • The Trace destination is configurable, it can be a file, it can be a console application and it supports also sending Log Message by mail.

To do so, this logging utility contains:

  • Interface called Logger.cs
  • Interface called ILogger.cs
  • Enumeration called LogType which presents the destination of trace messages

The class that implements the interface ILogger.cs:

Now let’s see how to configure the library through the configuration file. The configuration file will determine:

  1. The name of the source
  2. The type of destination message [a text file, a console file, etc…]
  3. The trace option output, in our case, the output of any trace containing this message
    • The type of trace [information, Error, warning, etc…]
    • Date Time of the trace
    • Thread ID
    • Process ID
    • TimeStamp

Testing the library:

Console_Display

If we want to customize the destination of trace messages, for example, display the trace message in a file system, we just add a ‘TextWriterTraceListener‘ to the configuration file:

Displaying trace messages in Bloc Notes.

TextListener

  1. You can customize the tracing output’s target by adding or removing TraceListener instances to or from the collection stored in the TraceSource.Listeners property. By default, trace output is produced using an instance of the DefaultTraceListener class. The preceding configuration file example demonstrates removing the DefaultTraceListener and adding a TextWriterTraceListener/ConsoleTraceListener to produce the trace output for the trace source.

    As a Microsoft developer, I have always been more comfortable when implementing my own libraries based on .NET prebuilt namespaces because I want to have absolute control on my source code, but I have seen many projects using Logging with third party libraries, for example, Log4Net. In the next paragraph, we will learn how to integrate this library into our applications.

II. Logging with Log4NET

The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. Log4net is a port of the log4j framework to Microsoft.NET platform. To integrate this library, you must use nuget package manager.

3_-_Add_Log4net_through_nuget

Like the TraceSource class, Log4net library enables the developer to customize message tracing (changing the log destinations and the format of messages). We will write two scenarios:

  • Default Configuration [Console Configuration]

4-_Default_Log4Net_configuration

  • Using custom configuration [saving messages into a text file].

XML Configuration file

Displaying trace messages in Bloc Notes.

5-FileAppender

Summary

Log4net is a port of Log4j to the .NET universe, Log4j is a popular logging framework and that’s why log4net has rapidly grown. The class System.Diagnostics.TraceSource provides high performance logging and tracing for applications but both use nearly the same mechanism.

I hope that you appreciated my effort. Thank you for viewing my blog post, try to download the source code and do not hesitate to leave your questions.