Project DescriptionMSBuild task for adding some logging to your application.
Inject calls to Log.Trace at the beginning of each method.
Integrates with nlog, log4net or your custom logger class within your assembly
This project is similar to
log4postsharp, except that this one is based on Mono and doesn't require you to use any proprietary or closed-source libraries.
Suppose, we have an instance of a logger as a static field in some class within our module (class name
Logger does matter! you can choose your own class name, but you should modify the config then (see details below))
internal static class Logger {
internal static log4net.ILog Log = LogManager.GetLogger("MyModule");
}
This project helps you inject calls to Logger.Log.Debug("Entering") to the beginning of each function in your module. This could really help in debugging some rarely-reproducible bugs on the client's PCs.
To do so you should define somewhere in your code two attributes:
public class LogAttribute : Attribute
{
public LogAttribute(bool logParameters = false)
{ }
}
public class NoLogAttribute : Attribute { }
so, if you have your program:
class Program
{
static void Main(string[] args)
{
Test();
}
public static void Test()
{
}
}
then add the following to the AssemblyInfo:
[assembly: Log]
and the following to the .csproj
<Import Project="..\Bin\Logging.NLog.targets" />
you'll got this in logs (when using NLog):
2011-02-24 22:46:38.9475|DEBUG|NlogConsoleApp.Program.Main|Entering
2011-02-24 22:46:38.9900|DEBUG|NlogConsoleApp.Program.Test|Entering
You can mark with
NoLog attribute the classes or methods you don't want to log, or omit the
Log attribute in
AssemblyInfo and mark with
Log only classes you want to explicitly log.
Classes or methods marked with
Log(true) gets logged with their parameters converted .ToString().
Predefined targets for log4net and nlog are shipped with project binaries.
You can easily adopt LoggingMagic library to any of your particular logger by only overriding one *Parameters class (see
NLogParameters for reference).
You can customize the logging targets to change the name of the static class, inside which your logger-instance is located, or rename Log and NoLog attributes to the desired names.
Below is an example filled with default values (part of Logging.NLog.targets)
<LoggingTask Assembly="@(IntermediateAssembly)" References="@(ReferencePath)" KeyFile="$(KeyOriginatorFile)" Injector="StaticFieldMethodCall" Parameters="NLog" ClassName='Logger' LogAttributeName='Log' NoLogAttributeName='NoLog' LogGetters='0' LogSetters='1' />