-
Notifications
You must be signed in to change notification settings - Fork 0
Logging Custom
Daniel edited this page Aug 16, 2020
·
9 revisions
To create and add your custom log service you need to know and implement a few base classes which are part of the framework. A new log service implementation always consists of three components (options, service, provider).
- Create the log service options
- Create the log service
- Create the log service provider
- Register your log service
The log service options are the interface between the framework and the user / developer. Here you can specify properties which can be set by the user and consumed by your log service.
Sample:
// Make sure to implement the LogOptions base class
public class CustomLogServiceOptions : LogOptions
{
public string Property
{
get => GetValue<string>();
set => SetValue(value);
}
}
Should contain the log message processing logic.
Sample:
// Make sure to implement the generic ScopedLogService base class
public class CustomLogService : ScopedLogService<CustomLogServiceOptions>
{
public CustomLogService(string context)
: base(context)
{
}
protected override void PublishMessage(LogMessage message)
{
// process the message
}
}
Should contain the logic to correctly instantiate your custom log service.
Sample:
// Make sure to implement the ILogServiceProvider interface
public class CustomLogServiceProvider : ILogServiceProvider
{
public IScopedLogService Create(string context)
{
return new CustomLogService(context);
}
}
To use your custom log service you just need to add it to the factory.
Sample:
// Make sure to implement the generic ScopedLogService base class
class Program
{
static void Main(string[] args)
{
LogServiceFactory.Instance.AddProvider<CustomLogServiceProvider>();
var logger = LogServiceFactory.Instance.Create(nameof(Program));
logger.Error("test");
}
}