PostSharp 5.0 Logging

Installing

  1. Add package PostSharp.Patterns.Diagnostics to any project that requires logging.
  2. Add the [Log] attribute to methods using Multicast, XML or IAspectProvider. See http://doc.postsharp.net/applying-aspects for details.
  3. Add the back-end package to your start-up project:
    • PostSharp.Patterns.Diagnostics.Backends.ApplicationInsights
    • PostSharp.Patterns.Diagnostics.Backends.AspNetLogging
    • PostSharp.Patterns.Diagnostics.Backends.CommonLogging
    • PostSharp.Patterns.Diagnostics.Backends.Console
    • PostSharp.Patterns.Diagnostics.Backends.EnterpriseLibrary
    • PostSharp.Patterns.Diagnostics.Backends.EventSource
    • PostSharp.Patterns.Diagnostics.Backends.Log4Net
    • PostSharp.Patterns.Diagnostics.Backends.NLog
    • PostSharp.Patterns.Diagnostics.Backends.Serilog
    • PostSharp.Patterns.Diagnostics.Backends.Trace
    • PostSharp.Patterns.Diagnostics.Backends.TraceSource
    • PostSharp.Patterns.Diagnostics.Backends.XamarinInsights
  4. Inside your application initialization (e.g. Program.Main in a console app), assign the PostSharp.Patterns.Diagnostics.LoggingServices.DefaultBackEnd
    property to an instance of the back-end class. Use IntelliSense to figure out the name of the logging back-end class. The name of this class ends with LoggingBackend.

Configuration Object Model

Example postsharp.config

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">

  <Logging xmlns="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics">
     <Profiles>
    <LoggingProfile Name="IncludeParameterTypeOnly" IncludeExceptionDetails="True">
      <DefaultOptions>
        <LoggingOptions  IncludeThisValue="False" IncludeParameterType="True" IncludeParameterName="False" IncludeParameterValue="False" IncludeDeclaringTypeName="False"/>
      </DefaultOptions>
      <ExceptionOptions>
        <LoggingOptions IncludeThisValue="False" IncludeParameterType="True" IncludeParameterName="False" IncludeParameterValue="False" IncludeDeclaringTypeName="False"/>
      </ExceptionOptions>
    </LoggingProfile>
   </Profiles>
  </Logging>
</Project>

The following sections document the object configuration object model. The XML maps directly to C#.
For more, see the API Reference in the PostSharp 5.0 Documentation.

Customization

Custom Formatters

  1. Derive a class from PostSharp.Patterns.Formatters.Formatter<T>.
  2. Register it using PostSharp.Patterns.Diagnostics.LoggingServices.Formatters.RegisterFormatter.

Custom Backends

  1. Derive a class from PostSharp.Patterns.Diagnostics.LoggingBackend. You can also derive from an available back-end, for instance Log4NetLoggingBackend.
  2. Derive a class from PostSharp.Patterns.Diagnostics.LogRecordBuilder and override the relevant methods.
  3. Override the CreateRecordBuilder method in your custom LoggingBackend class.
  4. Register your custom LoggingBackend class by setting the PostSharp.Patterns.Diagnostics.LoggingServices.DefaultBackEnd property.