User Tools

Site Tools


nebollogger

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
nebollogger [2021/08/07 02:27] nebolnebollogger [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://nuget.nebol.se for version history etc.
  
 === Quick example to give you an idea of how it works === === Quick example to give you an idea of how it works ===
  
-<code>+<code csharp>
 // First create the destination for the log output:  // First create the destination for the log output: 
 var dailyfile = new Destination_File(@"C:\TEMP","NebolLoggerDemo", "log", Rotation.Daily);  var dailyfile = new Destination_File(@"C:\TEMP","NebolLoggerDemo", "log", Rotation.Daily); 
Line 51: Line 53:
 == Destination_Console - outputs to console (stdout) == == Destination_Console - outputs to console (stdout) ==
  
-<code>+<code csharp>
 var dest =  new Destination_Console(); var dest =  new Destination_Console();
 </code> </code>
Line 57: 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) ==
  
-<code>+<code csharp>
 var dest =  new Destination_ColorConsole(); var dest =  new Destination_ColorConsole();
 </code> </code>
Line 63: Line 65:
 == Destination_File - outputs to file == == Destination_File - outputs to file ==
  
-<code>+<code csharp>
 var dest = new Destination_File(string folder,string suffix = "log", Rotation file_rolation = Rotation.None, Rotation folder_rotation = Rotation.None); var dest = new Destination_File(string folder,string suffix = "log", Rotation file_rolation = Rotation.None, Rotation folder_rotation = Rotation.None);
 </code> </code>
Line 71: 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 ==
  
-<code>+<code csharp>
 var dest = new Destination_Memory(); var dest = new Destination_Memory();
 </code> </code>
Line 77: Line 79:
 == Destination_Forwarder - forwards the output to one or more destinations == == Destination_Forwarder - forwards the output to one or more destinations ==
  
-<code>+<code csharp>
 var dest = new Destination_Forwarder(dest1, dest2, dest3...); var dest = new Destination_Forwarder(dest1, dest2, dest3...);
 </code> </code>
Line 83: 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 ==
  
-<code>+<code csharp>
 var dest = new Destination_Buffer(Destination dest, buffer_count = 100); var dest = new Destination_Buffer(Destination dest, buffer_count = 100);
 </code> </code>
Line 99: Line 101:
 Example on customizing output format using the delegate: Example on customizing output format using the delegate:
  
-<code>+<code csharp>
 dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item => dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item =>
  item.Date.ToString("yyyy-MM-dd HH:mm:ss ")  item.Date.ToString("yyyy-MM-dd HH:mm:ss ")
Line 109: Line 111:
 ); );
 </code> </code>
 +
 +For console output this might be desired formatting:
 +
 +<code csharp>
 +dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item =>
 + item.Date.ToString("HH:mm:ss")
 + + " : "
 + + item.Message
 +);
 +</code>
 +
  
 The method Entry.GetLogLevelAsText(item.Type) will return the 5 char length level text ie "TRACE" or "INFO " The method Entry.GetLogLevelAsText(item.Type) will return the 5 char length level text ie "TRACE" or "INFO "
Line 117: Line 130:
 == Filter_Level - limit output by level == == Filter_Level - limit output by level ==
  
-<code>+<code csharp>
 // this will create a new Filter_Level that limits output to messages of level Error (or higher/worse!) // this will create a new Filter_Level that limits output to messages of level Error (or higher/worse!)
 var filter = new Filter_Level(LogType.error); var filter = new Filter_Level(LogType.error);
Line 127: Line 140:
 == Filter_Context - limit output by context == == Filter_Context - limit output by context ==
  
-<code>+<code csharp>
 // create a context filter that accepts only context that contains "Main" // create a context filter that accepts only context that contains "Main"
 var filter = new Filter_Context("Main"); var filter = new Filter_Context("Main");
Line 135: Line 148:
 == Filter_Message - limit output by message == == Filter_Message - limit output by message ==
  
-<code>+<code csharp>
 // create a message filter that accepts only messages containing the string "localhost" // create a message filter that accepts only messages containing the string "localhost"
 var filter = new Filter_Message("localhost"); var filter = new Filter_Message("localhost");
Line 145: 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:
  
-<code>+<code csharp>
 public class Filter_Context : Filter public class Filter_Context : Filter
 { {
Line 166: Line 179:
 To log unhandled exceptions in .NET Framework, add this class: To log unhandled exceptions in .NET Framework, add this class:
  
-<code>+<code csharp>
 public class NebolUnhandledExceptionLogger : ExceptionLogger public class NebolUnhandledExceptionLogger : ExceptionLogger
 { {
Line 178: Line 191:
 and this line in WebApiConfig.Register(): and this line in WebApiConfig.Register():
  
-<code>+<code csharp>
 config.Services.Replace(typeof(IExceptionLogger), new NebolUnhandledExceptionLogger()); config.Services.Replace(typeof(IExceptionLogger), new NebolUnhandledExceptionLogger());
 </code> </code>
Line 185: Line 198:
 and in .NET Core: and in .NET Core:
  
-<code>+<code csharp>
 public class NebolErrorHandlingFilter : ExceptionFilterAttribute public class NebolErrorHandlingFilter : ExceptionFilterAttribute
 { {
Line 200: Line 213:
 then add the filter in Startup.cs ConfigureServices(): then add the filter in Startup.cs ConfigureServices():
  
-<code>+<code csharp>
 options.Filters.Add(new NebolErrorHandlingFilter()); options.Filters.Add(new NebolErrorHandlingFilter());
 </code> </code>
Line 210: Line 223:
 === Related packages === === Related packages ===
  
-NebolLogger.EntityFramework - for logging to database+  * [[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 - for logging to PushBullet recipient+NebolLogger.EntityFramework.Core - for logging to database using .NET Core
  
-NebolLogger.Controller - for accessing log data with a REST controller+OBSOLETE!  .NET Core is now supported in [[NebolLogger.EntityFramework]]
  
-NebolLogger.SharpRepository - for logging using SharpRepository 
  
nebollogger.1628296069.txt.gz · Last modified: 2021/08/07 02:27 by nebol