-
Notifications
You must be signed in to change notification settings - Fork 5
Logging
Logging is scope-based facade for logging application messages. These message can be of any type, the facade is not bound to text messages. Inside Neptuo
there are serializers for console and trace outputs, in Neptuo.Logging.Log4net there is implementation that uses behind the scene Log4net logging platform. Logging supports 5 standard levels of message importance.
Entry point for configuring logging facade is interface ILogFactory
. Instance implementing this contract provides instances of scope-based ILog
. This ILog
is the contract by passed through the application to provide support for logging application messages. Beside providing instance of logs, ILogFactory
also provides methods for registering log serializers.
These serializers takes care of writing message to outputs. As mentioned above, ConsoleSerializer
serializes passed messages to the Console.Out
, TraceSerializer
writers to Trace
and Log4net implementation uses full Log4net.
For simple text message logging, there are defined extension method for ILog
. These extensions are defines for all log levels and also provides extensions for simple string formatting. ILog
also defines method for determinig whether log level is active in at least log serializer. This method is usable when logging message required any time consuming operation to be done, so simple determining whether desired log level is active can save time when nothing will be logged.
Library is designed to use scopes of logs. This way you can simple join the chain of callings that results in message in log. For e.g.: When service is called from other service, which is called from HTTP request handler.
class HttpHandler
{
private readonly ILog log;
private readonly Service1 service1;
public HttpHandler(ILogFactory logFactory)
{
log = logFactory.Scope("HttpRequest");
service1 = new Service1(log.Factory);
}
public void Handle(HttpContext httpContext)
{
service1.Do();
}
}
class Service1
{
private readonly ILog log;
private readonly Service2 service2;
public Service1(ILogFactory logFactory)
{
log = logFactory.Scope("Service1");
service2 = new Service2(log.Factory);
}
public void Do()
{
service2.DoThat();
}
}
class Service2
{
private readonly ILog log;
public Service2(ILogFactory logFactory)
{
log = logFactory.Scope("Service2");
}
public void DoThat()
{
log.Debug("Some log message from Service2.");
}
}
Simply by-passing log instance and creating scopes in all constructors (or inside appropriate methods) of these classes, message log name can be like 'HttpRequest.Service1.Service2'. This provides two great features:
- You gain a 'call chain' even without callstack.
- You can simply write filter for messages that goes through 'Service1', or 'Service2' or 'HttpRequest'.
Log factory can serialize messages to any number of serializers. Default implementation of ILogFactory
is named DefaultLogFactory
and is mainly reusable for any kind of logging, and only the implementation of ILogSerializer
is required for including custom logging implementation.
For simple logs, like console, trace and possibly others, there are defined classes for simple message filtering and formatting. These classes are defined in namespaces Neptuo.Logging.Serialization.Filters
and Neptuo.Logging.Serialization.Formatters
.