This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| nebollogger [2021/08/07 02:13] – nebol | nebollogger [2021/11/30 02:13] (current) – nebol | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| NebolLogger is a C# package for quick and simple logging to database, file, or other destinations. | NebolLogger is a C# package for quick and simple logging to database, file, or other destinations. | ||
| + | |||
| + | See the nuget page at https:// | ||
| === 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(@" | ||
| - | // adding the destination to the logger Logger.Instance.Destinations.Add(dailyfile); | + | // adding the destination to the logger |
| + | Logger.Instance.Destinations.Add(dailyfile); | ||
| - | // while we're at it, let's output to the console as well, but only if it's an error or worse var console = new Destination_Console(); | + | // while we're at it, let's output to the console as well, but only if it's an error or worse |
| + | var console = new Destination_Console(); | ||
| + | console.Filters.Add(new Filter_Level(LogType.error)); | ||
| + | Logger.Instance.Destinations.Add(console); | ||
| - | // now we can use the logger in our program, this line will appear only in the file Logger.Instance.Trace(" | + | // now we can use the logger in our program, this line will appear only in the file |
| + | Logger.Instance.Trace(" | ||
| - | // this line will appear in both the file and in the console Logger.Instance.Error(" | + | // this line will appear in both the file and in the console |
| + | Logger.Instance.Error(" | ||
| </ | </ | ||
| + | |||
| + | === What information is contained in a log entry? === | ||
| + | |||
| + | * Type - the LogType aka " | ||
| + | * Date - the exact DateTime when the entry was created | ||
| + | * Context - the context of the message, usually the name of the method where it was created | ||
| + | * Line - the line number in the sourcecode where it was created | ||
| + | * Machine - a Machine object containing Name of the machine and an IP address | ||
| + | * Application - an Application object containing the name of the application | ||
| + | * Component - a Component object containing the entire path of the sourcecode file | ||
| + | |||
| + | === Levels === | ||
| + | |||
| + | Each message that is logged has one of these the levels of importance: | ||
| + | |||
| + | * Trace - least important - use this if you just want to follow the flow of your program | ||
| + | * Debug | ||
| + | * Information | ||
| + | * Notice | ||
| + | * Warning | ||
| + | * Error | ||
| + | * Critical | ||
| + | * Fatal - most important - the most severe kind of error | ||
| + | |||
| + | === Destinations === | ||
| + | |||
| + | == Destination_Console - outputs to console (stdout) == | ||
| + | |||
| + | <code csharp> | ||
| + | var dest = new Destination_Console(); | ||
| + | </ | ||
| + | |||
| + | ==Destination_ColorConsole - outputs to console (stdout) but with color (depending on level) == | ||
| + | |||
| + | <code csharp> | ||
| + | var dest = new Destination_ColorConsole(); | ||
| + | </ | ||
| + | |||
| + | == Destination_File - outputs to file == | ||
| + | |||
| + | <code csharp> | ||
| + | var dest = new Destination_File(string folder, | ||
| + | </ | ||
| + | |||
| + | Rotation can be : None, Hourly, Daily, Monthly, Yearly | ||
| + | |||
| + | == Destination_Memory - outputs to memory ie the data is lost at application exit == | ||
| + | |||
| + | <code csharp> | ||
| + | var dest = new Destination_Memory(); | ||
| + | </ | ||
| + | |||
| + | == Destination_Forwarder - forwards the output to one or more destinations == | ||
| + | |||
| + | <code csharp> | ||
| + | var dest = new Destination_Forwarder(dest1, | ||
| + | </ | ||
| + | |||
| + | == Destination_Buffer - outputs to a memory buffer that flushes its contents to an other destination after a limit has been reached == | ||
| + | |||
| + | <code csharp> | ||
| + | var dest = new Destination_Buffer(Destination dest, buffer_count = 100); | ||
| + | </ | ||
| + | |||
| + | == Create a custom destination == | ||
| + | |||
| + | To create a custom destination, | ||
| + | |||
| + | You may also override the Write(List< | ||
| + | |||
| + | === Customization === | ||
| + | |||
| + | The output format can be customized using Filters and using the FormattingDelegate delegate. You could also write your own class by inheriting from Destination. | ||
| + | |||
| + | Example on customizing output format using the delegate: | ||
| + | |||
| + | <code csharp> | ||
| + | dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item => | ||
| + | item.Date.ToString(" | ||
| + | + Entry.GetLogLevelAsText(item.Type) | ||
| + | + " " | ||
| + | + item.Context | ||
| + | + ": " | ||
| + | + item.Message | ||
| + | ); | ||
| + | </ | ||
| + | |||
| + | 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.GetLogLevelAsTrimmedText(item.Type) will remove any whitespace ie " | ||
| + | |||
| + | === Filters === | ||
| + | |||
| + | == Filter_Level - limit output by level == | ||
| + | |||
| + | <code csharp> | ||
| + | // this will create a new Filter_Level that limits output to messages of level Error (or higher/ | ||
| + | var filter = new Filter_Level(LogType.error); | ||
| + | |||
| + | // now attach the filter to a destination | ||
| + | dest.Filters.Add(filter); | ||
| + | </ | ||
| + | |||
| + | == Filter_Context - limit output by context == | ||
| + | |||
| + | <code csharp> | ||
| + | // create a context filter that accepts only context that contains " | ||
| + | var filter = new Filter_Context(" | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Filter_Message - limit output by message == | ||
| + | |||
| + | <code csharp> | ||
| + | // create a message filter that accepts only messages containing the string " | ||
| + | var filter = new Filter_Message(" | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Create a custom filter == | ||
| + | |||
| + | 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: | ||
| + | |||
| + | <code csharp> | ||
| + | public class Filter_Context : Filter | ||
| + | { | ||
| + | public string Context { get; set; } | ||
| + | |||
| + | public Filter_Context(string context) | ||
| + | { | ||
| + | Context = context; | ||
| + | } | ||
| + | |||
| + | public override bool Match(Entry entry) | ||
| + | { | ||
| + | return (entry.Context.Contains(Context)); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Logging unhandled exceptions === | ||
| + | |||
| + | To log unhandled exceptions in .NET Framework, add this class: | ||
| + | |||
| + | <code csharp> | ||
| + | public class NebolUnhandledExceptionLogger : ExceptionLogger | ||
| + | { | ||
| + | public override void Log(ExceptionLoggerContext context) | ||
| + | { | ||
| + | Logger.Instance.Error(context.Exception.ToString()); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | and this line in WebApiConfig.Register(): | ||
| + | |||
| + | <code csharp> | ||
| + | config.Services.Replace(typeof(IExceptionLogger), | ||
| + | </ | ||
| + | |||
| + | |||
| + | and in .NET Core: | ||
| + | |||
| + | <code csharp> | ||
| + | public class NebolErrorHandlingFilter : ExceptionFilterAttribute | ||
| + | { | ||
| + | public override void OnException(ExceptionContext context) | ||
| + | { | ||
| + | var exception = context.Exception; | ||
| + | Logger.Instance.Error(context.Exception.ToString()); | ||
| + | |||
| + | context.ExceptionHandled = true; // | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | then add the filter in Startup.cs ConfigureServices(): | ||
| + | |||
| + | <code csharp> | ||
| + | options.Filters.Add(new NebolErrorHandlingFilter()); | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | === Related packages === | ||
| + | |||
| + | * [[NebolLogger.EntityFramework]] | ||
| + | * [[NebolLogger.Controller]] | ||
| + | * [[NebolLogger.Mailer]] | ||
| + | * [[NebolLogger.PushBullet]] | ||
| + | * [[NebolLogger.SharpRepository]] | ||
| + | |||
| + | |||
| + | OBSOLETE! | ||
| + | |||
| + | NebolLogger.EntityFramework.Core - for logging to database using .NET Core | ||
| + | |||
| + | OBSOLETE! | ||
| + | |||
| + | |||