My favorites | Sign in
Project Logo
             
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 stefan.lieser, 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;

XmlConfigurator?(Watch = true)?

(log4postsharp caches the IsDebug/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.)

Comment by phillip.george, Mar 25, 2009

This is very good, although I would like it to log generic methods in the same way as the PostSharp? tracing example. Does anyone know if this is easily possible?

Comment by jim.twitchell, Sep 11, 2009

The source code link seems to be dead....

Comment by christian.crowhurst, Oct 21, 2009

It would be great if another token was added so that the message output by log4net included the ToString?() representation of the object that is being traced.

This would be particularly useful for tracing exceptions - the trace output could then include information about the object that is allowing an exception to bubble up. This would help identify the specific offending instance in an application.

I have added this enhancement request to the log4postsharp forum but didn't really get very far there so I am hoping that adding a comment here will reach the project owner!

The forum post: http://www.postsharp.org/forum/log4postsharp/enhancement-log-message-when-exception-thrown-t953.html


Sign in to add a comment
Hosted by Google Code