Log anything within your application with under ten lines of code.
- Simplicity:
It's really simple. Use the logger as a method attribute in your C# code. Everything else is already handled by the logger. - Implicit usage:
When you added the method attribute, it's done. Really. Don't worry about logging. It's done. - No performance impact:
The logging calls are compiled into the binary. The overhead is not more than a method call. You won't ever feel the difference. - Additional log types:
There are several different types of events that can be handled and filtered differently.
It's very simple. Just add the adapter (the "target" you're writing your logs to) using the following example:
Loki.UpdateAdapter(new BasicLoggerAdapter());
If an adapter should only be used by specific log levels:
Loki.UpdateAdapter(new BasicLoggerAdapter(), new List<LogLevel>{LogLevel.Debug});
You can also remove an adapter:
Loki.RemoveAdapter(adapter);
Of course, normal log messages using traditional explicit logging are supported as well:
Loki.Debug("Nobody cares about debug info");
Loki.Information("This may help finding problems");
Loki.Warning("This is relatively important");
Loki.Error("This is more important");
Loki.Critical("This is very important!");
You can also add any object to the method all, e.g.:
Loki.Warning("Maybe you have a problem:", new object());
Exceptions are very important. However, sometimes nobody cares about them. So, you can define this behavior by using the log levels accordingly.
Loki.ExceptionDebug("Nobody cares about debug info");
Loki.ExceptionInformation("This may help finding problems");
Loki.ExceptionWarning("This is relatively important");
Loki.ExceptionError("This is more important");
Loki.ExceptionCritical("This is very important!");
Returns are very important. Sometimes... ;)
The default log level of returns is Debug
.
Loki.Return(returnValue);
Just add Fody to your Project. Then add <LokiLogger/>
in the FodyWeavers.xml
. That's it. You're ready to go!
Add the attribute Loki
over every method or class you want to log. Every method call and exception will be logged, thanks to Fody. The execution time is additionally logged.
[Loki]
public int do(int x){
return x++;
}
Add the following snippet in the StartUp.cs
file in the method ConfigureServices
:
services.AddLokiObjectLogger();
Adapt and add the following part in the method Configure
in order to use LokiReporter
:
app.UseLokiLogger(
x => {
x.UseMiddleware = true;
x.Secret = "1234";
x.HostName = "<hostname>";
x.DefaultLevel = LogLevel.Debug;
}
);
By default, the three basic log events are:
Invoke
when entering a methodException
when an exception occursRest
when there happens a REST-callReturn
when returning a value
We always want to know, where an event happened. Thus, it's implemented using [CallerMemberName]
to avoid reflection overhead.