This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
nebollogger [2021/08/07 02:28] – nebol | nebollogger [2021/11/30 02:13] (current) – nebol | ||
---|---|---|---|
Line 7: | Line 7: | ||
=== Quick example to give you an idea of how it works === | === Quick example to give you an idea of how it works === | ||
- | < | + | < |
// First create the destination for the log output: | // First create the destination for the log output: | ||
var dailyfile = new Destination_File(@" | var dailyfile = new Destination_File(@" | ||
Line 53: | Line 53: | ||
== Destination_Console - outputs to console (stdout) == | == Destination_Console - outputs to console (stdout) == | ||
- | < | + | < |
var dest = new Destination_Console(); | var dest = new Destination_Console(); | ||
</ | </ | ||
Line 59: | Line 59: | ||
==Destination_ColorConsole - outputs to console (stdout) but with color (depending on level) == | ==Destination_ColorConsole - outputs to console (stdout) but with color (depending on level) == | ||
- | < | + | < |
var dest = new Destination_ColorConsole(); | var dest = new Destination_ColorConsole(); | ||
</ | </ | ||
Line 65: | Line 65: | ||
== Destination_File - outputs to file == | == Destination_File - outputs to file == | ||
- | < | + | < |
var dest = new Destination_File(string folder, | var dest = new Destination_File(string folder, | ||
</ | </ | ||
Line 73: | Line 73: | ||
== Destination_Memory - outputs to memory ie the data is lost at application exit == | == Destination_Memory - outputs to memory ie the data is lost at application exit == | ||
- | < | + | < |
var dest = new Destination_Memory(); | var dest = new Destination_Memory(); | ||
</ | </ | ||
Line 79: | Line 79: | ||
== Destination_Forwarder - forwards the output to one or more destinations == | == Destination_Forwarder - forwards the output to one or more destinations == | ||
- | < | + | < |
var dest = new Destination_Forwarder(dest1, | var dest = new Destination_Forwarder(dest1, | ||
</ | </ | ||
Line 85: | Line 85: | ||
== Destination_Buffer - outputs to a memory buffer that flushes its contents to an other destination after a limit has been reached == | == Destination_Buffer - outputs to a memory buffer that flushes its contents to an other destination after a limit has been reached == | ||
- | < | + | < |
var dest = new Destination_Buffer(Destination dest, buffer_count = 100); | var dest = new Destination_Buffer(Destination dest, buffer_count = 100); | ||
</ | </ | ||
Line 101: | Line 101: | ||
Example on customizing output format using the delegate: | Example on customizing output format using the delegate: | ||
- | < | + | < |
dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item => | dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item => | ||
item.Date.ToString(" | item.Date.ToString(" | ||
Line 111: | Line 111: | ||
); | ); | ||
</ | </ | ||
+ | |||
+ | For console output this might be desired formatting: | ||
+ | |||
+ | <code csharp> | ||
+ | dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item => | ||
+ | item.Date.ToString(" | ||
+ | + " : " | ||
+ | + item.Message | ||
+ | ); | ||
+ | </ | ||
+ | |||
The method Entry.GetLogLevelAsText(item.Type) will return the 5 char length level text ie " | The method Entry.GetLogLevelAsText(item.Type) will return the 5 char length level text ie " | ||
Line 119: | Line 130: | ||
== Filter_Level - limit output by level == | == Filter_Level - limit output by level == | ||
- | < | + | < |
// this will create a new Filter_Level that limits output to messages of level Error (or higher/ | // this will create a new Filter_Level that limits output to messages of level Error (or higher/ | ||
var filter = new Filter_Level(LogType.error); | var filter = new Filter_Level(LogType.error); | ||
Line 129: | Line 140: | ||
== Filter_Context - limit output by context == | == Filter_Context - limit output by context == | ||
- | < | + | < |
// create a context filter that accepts only context that contains " | // create a context filter that accepts only context that contains " | ||
var filter = new Filter_Context(" | var filter = new Filter_Context(" | ||
Line 137: | Line 148: | ||
== Filter_Message - limit output by message == | == Filter_Message - limit output by message == | ||
- | < | + | < |
// create a message filter that accepts only messages containing the string " | // create a message filter that accepts only messages containing the string " | ||
var filter = new Filter_Message(" | var filter = new Filter_Message(" | ||
Line 147: | Line 158: | ||
You can also create your own filter by inheriting from Filter and overriding the Match() method. As an example, here is the complete code for Filter_Context: | You can also create your own filter by inheriting from Filter and overriding the Match() method. As an example, here is the complete code for Filter_Context: | ||
- | < | + | < |
public class Filter_Context : Filter | public class Filter_Context : Filter | ||
{ | { | ||
Line 168: | Line 179: | ||
To log unhandled exceptions in .NET Framework, add this class: | To log unhandled exceptions in .NET Framework, add this class: | ||
- | < | + | < |
public class NebolUnhandledExceptionLogger : ExceptionLogger | public class NebolUnhandledExceptionLogger : ExceptionLogger | ||
{ | { | ||
Line 180: | Line 191: | ||
and this line in WebApiConfig.Register(): | and this line in WebApiConfig.Register(): | ||
- | < | + | < |
config.Services.Replace(typeof(IExceptionLogger), | config.Services.Replace(typeof(IExceptionLogger), | ||
</ | </ | ||
Line 187: | Line 198: | ||
and in .NET Core: | and in .NET Core: | ||
- | < | + | < |
public class NebolErrorHandlingFilter : ExceptionFilterAttribute | public class NebolErrorHandlingFilter : ExceptionFilterAttribute | ||
{ | { | ||
Line 202: | Line 213: | ||
then add the filter in Startup.cs ConfigureServices(): | then add the filter in Startup.cs ConfigureServices(): | ||
- | < | + | < |
options.Filters.Add(new NebolErrorHandlingFilter()); | options.Filters.Add(new NebolErrorHandlingFilter()); | ||
</ | </ | ||
Line 212: | Line 223: | ||
=== Related packages === | === Related packages === | ||
- | NebolLogger.EntityFramework | + | * [[NebolLogger.EntityFramework]] |
+ | * [[NebolLogger.Controller]] | ||
+ | * [[NebolLogger.Mailer]] | ||
+ | * [[NebolLogger.PushBullet]] | ||
+ | * [[NebolLogger.SharpRepository]] | ||
- | NebolLogger.EntityFramework.Core - for logging to database using .NET Core | ||
- | NebolLogger.Mailer - for logging to mail | + | OBSOLETE! |
- | NebolLogger.PushBullet | + | NebolLogger.EntityFramework.Core |
- | NebolLogger.Controller - for accessing log data with a REST controller | + | OBSOLETE! |
- | NebolLogger.SharpRepository - for logging using SharpRepository | ||