|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionLog files are crucial when it comes to troubleshooting an application or finding the exact source of a bug. Unfortunately, the more information the application logs, the more its code gets cluttered with logging statements. Readability of source code decreases and, in a team of developers, it may be hard to keep consistency of logged data. If these problems sound familiar to you, Log4PostSharp is for you. It inserts logging statements without touching your source code. It injects calls to log4net methods into the DLL and EXE files automatically, right after they are compiled. This guarantees consistency of log files, and allows you to forget about logging at all. Quick comparisonTo illustrate the difference Log4PostSharp makes, this section compares two snippets of code. In the first snippet, the logging has been added in the traditional way: public void Save()
{
try
{
if (log.IsDebugEnabled)
{
log.Debug("Saving file.");
}
Configuration.Save();
if (log.IsDebugEnabled)
{
log.Debug("File has been saved.");
}
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
{
log.Error("Failed to save file.", ex);
}
throw;
}
}
Note that, out of the total 10 lines of code (not counting the lines containing only braces), only 1 line has any business value (the one in bold). The remaining 9 lines are there only to ensure that the progress of the method execution gets logged. And, this is only a single method. Real-life applications consist of thousands of them. That means, a lot of lines which introduce no business value and make the code harder to read. This is where Log4PostSharp comes to rescue. Check the second snippet; it shows method that performs exactly the same business action as the first one, but relies on Log4PostSharp to do the logging: [Log]
public void Save()
{
Configuration.Save();
}
It does not contain even a single logging statement, but still produces log output which is very similar to the first version of the method. And, as the latter part of this article will show, even the DemoIf you want to quickly see some sample application that uses Log4PostSharp, you can download the demo project from here. Remember to download and install the latest version of PostSharp first, from here. How does it work?To achieve its goal, Log4PostSharp uses PostSharp, an excellent tool that allows injecting code into .NET assemblies. Normally, the injection occurs automatically after the project is compiled (the PostSharp installer configures a .NET build process to make this possible; for more details, please visit this website). To see how the injection works, first see the [Log(EntryLevel = LogLevel.Debug,
ExitLevel = LogLevel.Debug, ExceptionLevel = LogLevel.Fatal)]
private static int ReadNumber() {
// Display prompt.
Console.Write("Please enter a number: ");
// Read the line from the console.
string line = Console.ReadLine();
// Convert the data into the integer.
return int.Parse(line, CultureInfo.CurrentCulture);
}
It does not contain any logging statements – just three lines of code and the private static int ReadNumber()
{
int ~returnValue~2;
try
{
if (~log4PostSharp~isDebugEnabled)
{
~log4PostSharp~log.Debug("Entering method: Int32 ReadNumber().");
}
Console.Write("Please enter a number: ");
int CS$1$0000 = int.Parse(Console.ReadLine(),
CultureInfo.CurrentCulture);
~returnValue~2 = CS$1$0000;
if (~log4PostSharp~isDebugEnabled)
{
~log4PostSharp~log.Debug("Exiting method: Int32 ReadNumber().");
}
}
catch (Exception ~ex~3)
{
if (~log4PostSharp~isFatalEnabled)
{
~log4PostSharp~log.Fatal("Exception thrown " +
"from method: Int32 ReadNumber().", ~ex~3);
}
throw;
}
return ~returnValue~2;
}
Note that the The code that gets injected follows the log4net recommendations, and is optimized to achieve the best performance. In the static constructor, the logger is created for every class, as: ~log4PostSharp~log = LogManager.GetLogger(typeof(Program));
This means that the logger name is the same as the class name (including namespace). Log4PostSharp does not interfere with any manually added logging code, and requires that the developer configures log4net the usual way. Note: Remember that Log4PostSharp caches some information (like indications whether loggers are enabled). If you configure log4net by placing the " CustomizationThe EntryLevel, ExitLevel, ExceptionLevelValues of these properties are of EntryText, ExitText, ExceptionTextDetermines the text that gets logged. It can contain placeholders which then get replaced with actual values:
Note that placeholders are marked as being either weave-time or runtime. Values of the weave-time placeholders are determined when the code is being injected. Hence, the performance of the generated code is exactly the same as if the placeholders were not used (build may take a little longer though). On the other hand, values of the runtime ones cannot be determined until the method gets called (and may vary between two different method calls). Therefore, if runtime placeholders are specified, the code generated by Log4PostSharp needs to find proper values and then put them into the message. Such code looks like: if (log.IsDebugEnabled)
{
object[] args = new object[] { left, top, width, height };
log.DebugFormat(CultureInfo.InvariantCulture,
"Drawing box: '{0}', '{1}', '{2}', '{3}'.", args);
}
Examples of messages using placeholders:
Notes and limitations:
MulticastAdding the attribute to every method separately would not only be tedious but would also defeat one of the biggest benefits of the library: automatization. Fortunately, PostSharp features custom attribute multicasting, which allows applying any attribute to multiple methods at once. You can use the feature by dropping the following line into the AssemblyInfo.cs file: [assembly: Log(AttributeTargetTypes = "*", EntryLevel = LogLevel.Debug,
ExitLevel = LogLevel.Debug, ExceptionLevel = LogLevel.Error, AttributePriority = 1)]
The line adds the attribute to every single method in the assembly. This also includes property setters and getters and event addition and removal handlers. To exclude them, add the following lines: [assembly: Log(AttributeTargetTypes = "*",
AttributeTargetMembers = "get_*", AttributeExclude = true, AttributePriority = 2)]
[assembly: Log(AttributeTargetTypes = "*",
AttributeTargetMembers = "add_*", AttributeExclude = true, AttributePriority = 2)]
[assembly: Log(AttributeTargetTypes = "*",
AttributeTargetMembers = "remove_*", AttributeExclude = true, AttributePriority = 2)]
The lines prevent the attribute from being added to property getters, and event addition, and removal handlers, respectively. Note the Multicast attributes can be customized just like the ordinary ones. How to use it in own projectsIf you have a project where you want to use automated logging, you have to follow a few simple steps. Get librariesDownload PostSharp from here and install it. Download Log4PostSharp from here and copy the Log4PostSharp.Weaver.dll and Log4PostSharp.psplugin files into the Plugins directory under your PostSharp installation folder. Copy the Log4PostSharp.dll into the directory where you store other libraries for your project. Add referencesIn the project, add references to the Log4PostSharp.dll and PostSharp.Public.dll (you can find this library on the ".NET" tab of the "Add reference..." dialog). Remember that these DLL files are required only by the compiler and the weaver. You do not need to deploy them with your application or library. Decorate methods with attributeDecorate the desired methods with the Other optionsIf you do not want to install Log4PostSharp into the PostSharp Plugins folder, you will need to provide a .psproj file for your project. For more details, please refer to the PostSharp documentation. Final wordsThe Log4PostSharp is published under the BSD license which allows using it freely even in commercial products.
|
||||||||||||||||||||||