Analytics


Google

Sunday, May 25, 2008

Application Logging

Some new classes were introduced to facilitate application logging. Previously, we had to depend on 3rd party libraries like log4net. One of the classes that are most useful is the FileLogTraceListener which provided autoarchiving by date or by application.

I started exploring the information using information from the this article.

First lets start with the app.config file:

<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="MSwitch">
<listeners>
<add name="LogToFile" />
</listeners>
</source>
</sources>
<switches>
<add name="MSwitch" value="All" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
<add name="LogToFile" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"
logFileCreationSchedule="Daily"
traceOutputOptions="DateTime"
BaseFileName="W_Test"
location="Custom"
CustomLocation="c:\log\"
Delimiter ="|"
AutoFlush="True"
Append="True"
/>
</sharedListeners>
</system.diagnostics>

To use be able to use My.Application.log class, you need a source called DefaultSource, you can then point it to a switch name. In the switch name (or place the value directly using SwitchValue, you have an option to identify the level of logging (taken from the help file) :

ActivityTracing Enumeration
Description
Off Does not allow any events through.
Critical Allows only Critical events through.
Error Allows Critical and Error events through.
Warning Allows Critical, Error, and Warning events through.
Information Allows Critical, Error, Warning, and Information events through.
Verbose Allows Critical, Error, Warning, Information, and Verbose events through.
ActivityTracing Allows the Stop, Start, Suspend, Transfer, and Resume events through.
All Allows all events through.


You can then add various listeners. If you are using FileLogTraceListener,
  • you can specify the BaseFileName. This will be the used as your log file name.
  • specify whether or not to append to the log.
  • autoflush
  • location (if specify Custom then specify CustomLocation to tell it where to put the file).
  • logFileCreationSchedule whether to create Daily, Weekly or None.
  • Delimiter (character to use as delimiter between the columns in the log file).
  • TraceOutputOption (additional items to automatically add to the log file). If you use DateTime, it will provide it give you the time in GMT timezone.
Based on the above configuration, my log file will have the name W_Test-2008-05-24.log and the content will look something like this:

DefaultSource|Start|4|Entering Button2_click|2008-05-24 03:37:54Z
DefaultSource|Stop|5|Leaving Button2_click|2008-05-24 03:37:54Z
DefaultSource|Information|0|Going thru Button2|2008-05-24 03:37:54Z

3 comments:

elber said...

Hola, ¿en que tipo de aplicación lo utilizas? yo estoy intentando utilizarlo en la creación de un servicio windows, pero al definir la entrada en sharedListeners no me reconoce los atributos location y customlocation.

No sé si será porque se trata de una aplicación de servicio windows y no es una aplicación windows normal, o quizá porque tengo que crear alguna referencia que no tengo, pero lo he probado todo.

¿Se te ocurre algún motivo?

Gracias por adelantado.

Strovek said...

Translation of what elber said:

"Hello, Is that what type of application using? I am trying to use it in creating a service windows, but to define the entry into sharedListeners I do not recognize the attributes and location customlocation.

I do not know if it will be because this is an application service windows and is not a normal application windows, or perhaps because I have to create some reference that I have not, but I've tried everything.

Is there some reason you happen?

Thanks in advance."

Strovek said...

It should work for any type of application. First thing to do to use the shared listener is to declare a TraceSource as follows:

Dim ts As New TraceSource("clsTrace")

That is assuming your config file contains the following:

<source name="MTFTrace" switchName="InfoSwitch">
<listeners>
<add name="LogToFileCls" />
</listeners>
</source>

Where the LotToFileCls is as follows:

<add name="LogToFileMTF" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" logFileCreationSchedule="Daily" traceOutputOptions="DateTime" BaseFileName="clsObj" location="Custom" CustomLocation="c:\log\" Delimiter="," AutoFlush="True" Append="True" />

Subsequently, you need to perform the following to log the item to your log file:

ts.TraceEvent(TraceEventType.Information, 0, msgStr)

I hope this helps. Let me know if you need further help.