Jean Hibbert's Blog

.NET Framework, SQL Server and other random thoughts.

July 2008 - Posts

Log4PostSharp : How to set it up

After an hour of banging my head against the wall and much dissapointment at the amount of documentation out there on such a usefull tool as Log4PostSharp I've decided that it would be a good idea to get off my lazy .... and write a short blog post on Log4PostSharp.

Firstly let me give you a quick description of what Log4PostSharp is: 

Log4PostSharp is an Aspect-Oriented tool that allows you to log when a

1) method is entered

2) method is exited

3) when a method throws an exception

Methods that you would like to implement this logging functionality on, only have to be decorated by a Log attribute from the Log4PostSharp namespace.

 
Steps to set up An application to use Log4PostSharp:

1) Download the source code for the Log4Post sharp project from http://postsharp-user-plugins.googlecode.com/svn/trunk/Log4PostSharp/

2) Optionally you can compile the project in VS2008 - There is a \Libraries folder that contains all though if you are still stuck with VS 2005 (like the 95% of the working population :-) )

3) Download and install the PostSharp Aspect Oriented tool from http://www.postsharp.org/.

4) Copy Log4PostSharp.psplugin and Log4PostSharp.Weaver.dll from the Libraries folder in the Log4PostSharp project into the plugins folder created by the PostSharp installation which would normally be at C:\Program Files\PostSharp 1.0\PlugIns

5)  Then in your project: Add references to the following libraries : log4net.dll (from the log4net project) + log4PostSharp.dll (from the log4postsharp project) + PostSharp.Public.dll (from the PostSharp project)

6)  Update your AssemblyInfo.cs file with:

 

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

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4PostSharpTest.exe.log4net.config", Watch = true)]

As you can see Log4PostSharpTest.exe.log4net.config is the name of the config file that will hold the log4net configuration settings. 

7) Create a configuration file Log4PostSharpTest.exe.log4net.config and insert the log4net configuration settings: eg

 <?xml version="1.0" encoding="utf-8" ?>

<log4net>

     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

           <param name="File" value="trace.log" />

           <param name="AppendToFile" value="true" />

           <param name="MaxSizeRollBackups" value="5" />

           <param name="MaximumFileSize" value="100KB" />

           <param name="RollingStyle" value="Size" />

           <param name="StaticLogFileName" value="true" />

           <layout type="log4net.Layout.PatternLayout,log4net">

                <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />

           </layout>

     </appender>

     <root>

           <level value="DEBUG" />

           <appender-ref ref="RollingLogFileAppender" />

     </root>

</log4net>

 8) Finally decorate the methods in your application that you would like to treack with the logging functionality by using the log attribute as mentioned earlier eg:

 

        [Log(EntryLevel = LogLevel.Debug, EntryText = "Test for '{@i1}', '{@i2}', '{@r1}', '{@r2}'.")]

        [Log(ExitLevel = LogLevel.Fatal, ExitText = "Was called with params: {paramvalues}.")]

        [Log(ExceptionLevel = LogLevel.Fatal, ExceptionText = "Exception occured with parameters : '{@i1}', '{@i2}', '{@r1}', '{@r2}'.")]

9) Run the application and have a look at the magic in the trace.log file output. Since I had my debug setting set to DEBUG, every every event handled by the log4postsharp framework was logged. 

So my output looked like:

2008-07-24 15:49:32,726 [1] DEBUG Log4PostSharpTest.Program [(null)] - Entering method: Void Main(System.String[]).
2008-07-24 15:49:32,757 [1] DEBUG Log4PostSharpTest.Program [(null)] - Entering method: Void MethodWithNoReturnValue().
2008-07-24 15:49:32,757 [1] DEBUG Log4PostSharpTest.Program [(null)] - Exiting method: Void MethodWithNoReturnValue().
2008-07-24 15:49:32,757 [1] FATAL Log4PostSharpTest.Program [(null)] - Calling eebd09aa-c47c-4f2e-a21e-e90d8030ed6a.
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Entering method: Void MethodWithNoReturnValue(System.Guid).
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Exiting method: Void MethodWithNoReturnValue(System.Guid).
2008-07-24 15:49:32,773 [1] FATAL Log4PostSharpTest.Program [(null)] - Return value is: ''.
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Test for '1', 'A', '10', 'X'.
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Entering method: Int32 MultipleArgsMethod(Int32, System.String, Int32 ByRef, System.Object ByRef, System.Guid ByRef, System.String ByRef).
2008-07-24 15:49:32,773 [1] FATAL Log4PostSharpTest.Program [(null)] - Return value is: '51'.
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Exiting method: Int32 MultipleArgsMethod(Int32, System.String, Int32 ByRef, System.Object ByRef, System.Guid ByRef, System.String ByRef).
2008-07-24 15:49:32,773 [1] FATAL Log4PostSharpTest.Program [(null)] - Was called with params: "1", "A", "10", "X", "6fef8a16-9373-490c-bf07-3d47feda5c31", "X".
2008-07-24 15:49:32,773 [1] INFO  Log4PostSharpTest.Program [(null)] - Test message.
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Entering method: System.Guid MethodWithGuidReturnValue().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Exiting method: System.Guid MethodWithGuidReturnValue().
2008-07-24 15:49:32,773 [1] INFO  Log4PostSharpTest.Program [(null)] - Doing stuff.
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Entering method: Int32 MethodWithIntReturnValue().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Exiting method: Int32 MethodWithIntReturnValue().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program+AnotherSubprogram`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] [(null)] - Entering method: Void .cctor().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program+AnotherSubprogram`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] [(null)] - Exiting method: Void .cctor().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program+AnotherSubprogram`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] [(null)] - Entering method: Void .ctor().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program+AnotherSubprogram`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] [(null)] - Exiting method: Void .ctor().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program+AnotherSubprogram`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] [(null)] - Entering method: U Act[U]().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program+AnotherSubprogram`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] [(null)] - Exiting method: U Act[U]().
2008-07-24 15:49:32,773 [1] DEBUG Log4PostSharpTest.Program [(null)] - Exiting method: Void Main(System.String[]).
 

Additional information and references:

- http://www.postsharp.org/

- http://postsharp-user-plugins.googlecode.com/svn/wiki/Log4PostSharp.wiki

- http://www.postsharp.org/forum/log4postsharp/

- http://haacked.com/archive/2006/08/08/Log4NetAndExternalConfigurationFileInASP.NET2.0.aspx

- http://code.google.com/p/postsharp-user-plugins/wiki/Log4PostSharp

 
And finally a big thank you to PostSharp for the AOP tool and especially Michal Dabrowski for writing the Log4PostSharp addin. Thanks also to my boss Eden that introduced me to this tool.

I'll be updating this post with more information in the near future. You can either follow the instructions above or you can download my Test application from here.

Happy coding.
Jean

 


 

The Matrix - "Temet Nosce"

I'd say I'm one of those people with an ingrained skeptical view of "societies view of what is write and wrong". My skepticism is also based on the idea that most authority figures are not necessarily basing their decisions on what they truly believe is the greater good for humanity as a whole, but rather how can they can achieve their own personal goals.

I think it all boils down to my negative perception of the way human's think.

It's as though we don't want to survive anymore we want to conquer and devour everything. If you look at how we are destroying the planet, how our greed for material goods consumes us.... How much does a human need to live happily... and the answer is.... not much. Another issue I have with the human race is how blindly we conform to societies "norms". How rarely we actually stand back and ask ourselves whether what we are doing is really necessary, rather than just getting consumed by the rat race.