What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated Jun 05, 2008 by gael.fraiteur
Labels: Featured
Log4PostSharp  
Describes how to start using Log4PostSharp

For questions, please visit the support forum.

Introduction

Log4PostSharp contains an aspect which uses PostSharp to inject MSIL code that writes messages to log4net log. This page describes shortly how to get started using it.

Requirements

Installing Log4PostSharp

  • Download Log4PostSharp source code using an SVN client.
  • Build the solution using Visual Studio or MSBuild.
  • Optionally: copy files Log4PostSharp.psplugin and Log4PostSharp.Weaver.dll into C:\Program Files\PostSharp 1.0\PlugIns (for global installation) or C:\Documents and Settings\userName\Application Data\PostSharp 1.0 (user-only installation).

Log4PostSharp does not require any specific log4net configuration - configure it your usual way.

Using Log4PostSharp

Adding Log4PostSharp to your project

Add to your project a reference to assemblies PostSharp.Public.dll and Log4PostSharp.dll.

If you did not install the plug-in as described here above, add the directory containing Log4PostSharp.psplugin in the in Reference Paths, in Visual Studio's Project Properties.

Logging methods

Code injected by Log4PostSharp can log three kinds of messages:

  • when a method is entered,
  • when a method is exited without throwing exception,
  • when a method throws an exception.

Logging a single method

The only thing that developer has to do is to decorate a method with LogAttribute and provide values for few attribute properties, like in the following example:

[Log(LogLevel.Info, "Counting characters.")]
int CountCharacters(string arg) {
   return arg.Length;
}

This would cause that when the method is entered, "Counting characters." message is logged with severity level of Info. Also, if exception occurs in the method, it would be logged with level of Error.

Logging multiple methods (Multicast)

In most cases it is desirable to add the following line to project's AssemblyInfo.cs file:

[assembly: Log(AttributeTargetTypes = "*", EntryLevel = LogLevel.Debug, ExitLevel = LogLevel.Debug, ExceptionLevel = LogLevel.Error)]

This causes that all three kinds of events are logged and method signatures are appended to log messages.

Remarks

Use Multicast feature of the PostSharp to apply the attribute to all methods in order to log messages with Debug level. Besides of this you may additionally decorate chosen methods with Info level to log more important messages.

For questions, please visit the support forum.


Comment by blahetal, Mar 08, 2008

thanks, this is really useful btw your code is pretty hardcore :)

Comment by slie...@lieser-online.de, Aug 29, 2008

The Link to the forum should be http://www.postsharp.org/forum/log4postsharp/

Cheers, Stefan Lieser

Comment by rhysevancampbell, Sep 08, 2008

We are building for some odd build machine so we need the reference path NOT in a user file (which the add refence in Reference Paths, in Visual Studio's Project Properties does)

By adding

<referencepath>
\..\References\
</referencepath>
to your csproj file you seem to able to get the same result without relying on a .csproj.user file

E.G.

<project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<propertygroup>
<configuration Condition=" '$(Configuration)' == '' ">
Debug
</configuration>
<platform Condition=" '$(Platform)' == '' ">
AnyCPU
</platform>
<productversion>
8.0.50727
</productversion>
<schemaversion>
2.0
</schemaversion>
<projectguid>
{5E316943-547E-4974-A45F-2856B9752632}
</projectguid>
<outputtype>
Library
</outputtype>
<referencepath>
\..\References\
</referencepath>

etc etc

Cheers RhysC

Comment by gerben.oostra, Sep 11, 2008

I spent a day trying to find out why Log4PostSharp did not work (i thought the plugin did not run). After checking it with reflector, the code was injected. However, my manual log4net xml configure loader ran too late. Thus, to be able to actually get log output (thanx postsharp forum!), add the following to your assemblyinfo.cs;

[assembly: XmlConfigurator??(Watch = true)]

(log4postsharp caches the Is[Debug/Info/..]Enabled values in the static constructor of the being-logged class (injected in front of your own static constructor code). The injected code only executes ILog.Info if IsInfoEnabled?? is true. So you have to load your log4net configuration before the static constructors.)


Sign in to add a comment