|
Log4PostSharp
Describes how to start using Log4PostSharp
For questions, please visit the support forum. IntroductionLog4PostSharp 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
Log4PostSharp does not require any specific log4net configuration - configure it your usual way. Using Log4PostSharpAdding Log4PostSharp to your projectAdd 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 methodsCode injected by Log4PostSharp can log three kinds of messages:
Logging a single methodThe 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. RemarksUse 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. |
Sign in to add a comment

thanks, this is really useful btw your code is pretty hardcore :)
The Link to the forum should be http://www.postsharp.org/forum/log4postsharp/
Cheers, Stefan Lieser
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 fileE.G.
<project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">etc etc
Cheers RhysC
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.)