|
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 file
E.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;
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.)
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?
The source code link seems to be dead....
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