Logging aspect support for MVVM
$ dotnet add package OutWit.Common.LoggingInstall-Package OutWit.Common.Logging
or
> dotnet add package OutWit.Common.Logging
Log AspectMaintaining logs is critical for debugging and supporting complex applications. Manually adding logging calls everywhere can be tedious and error-prone. The OutWit.Common.Logging package simplifies this process with three powerful aspects: Log, NoLog, and Measure. The source code is available here.
Before using the logging aspects, initialize the logger. The current implementation is based on Serilog:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Information)
.Enrich.WithExceptionDetails()
.WriteTo.File(@"D:\Log\Log.txt",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
fileSizeLimitBytes: 524288)
.CreateLogger();
Log AspectThe Log aspect can be applied to individual methods:
public class Model
{
[Log]
public void DoSomething1()
{
}
public void DoSomething2()
{
}
}
Or to an entire class:
[Log]
public class Model
{
public void DoSomething1()
{
}
public void DoSomething2()
{
}
}
When applied to a class, it automatically applies to all methods within the class.
What Does the Log Aspect Do?
This flexibility allows you to control log verbosity by adjusting the logger's configuration.
If the Log aspect is applied to a class, but you want to exclude specific methods from logging, you can use the NoLog aspect:
[Log]
public class Model
{
public void DoSomething1()
{
}
[NoLog]
public void DoSomething2()
{
}
}
To measure the execution time of a method, apply the Measure aspect. This will log the duration in milliseconds:
public class Model
{
public void DoSomething1()
{
}
[Measure]
public void DoSomething2()
{
}
}
When DoSomething2 is called, the log will include the method’s execution time.
For more details, check out the article.
Licensed under the Apache License, Version 2.0. See LICENSE.
If you use OutWit.Common.Logging in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.Logging (https://ratner.io/)".
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
You may not: