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.
// First create the destination for the log output: var dailyfile = new Destination_File(@"C:\TEMP","NebolLoggerDemo", "log", Rotation.Daily); // 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(); 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("Testing the new logger, it's working nicely!"); // this line will appear in both the file and in the console Logger.Instance.Error("Oops, something went wrong!");
Each message that is logged has one of these the levels of importance:
var dest = new Destination_Console();
var dest = new Destination_ColorConsole();
var dest = new Destination_File(string folder,string suffix = "log", Rotation file_rolation = Rotation.None, Rotation folder_rotation = Rotation.None);
Rotation can be : None, Hourly, Daily, Monthly, Yearly
var dest = new Destination_Memory();
var dest = new Destination_Forwarder(dest1, dest2, dest3...);
var dest = new Destination_Buffer(Destination dest, buffer_count = 100);
To create a custom destination, create a class that inherits from Destination, then override the Write(Entry entry) method. That's the method that is called to perform the actual writing of the entry to the destination.
You may also override the Write(List<Entry> entries) if you want to provide special logic to handle writing a list of entries all at once. Otherwise the Write(Entry entry) method is called once for every item in the list.
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:
dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item => item.Date.ToString("yyyy-MM-dd HH:mm:ss ") + Entry.GetLogLevelAsText(item.Type) + " " + item.Context + ": " + item.Message );
For console output this might be desired formatting:
dest.FormattingDelegate = new NebolLoggerFormattingDelegate(item => item.Date.ToString("HH:mm:ss") + " : " + item.Message );
The method Entry.GetLogLevelAsText(item.Type) will return the 5 char length level text ie “TRACE” or “INFO ” The method Entry.GetLogLevelAsTrimmedText(item.Type) will remove any whitespace ie “TRACE” or “INFO”
// 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); // now attach the filter to a destination dest.Filters.Add(filter);
// create a context filter that accepts only context that contains "Main" var filter = new Filter_Context("Main");
// create a message filter that accepts only messages containing the string "localhost" var filter = new Filter_Message("localhost");
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 string Context { get; set; } public Filter_Context(string context) { Context = context; } public override bool Match(Entry entry) { return (entry.Context.Contains(Context)); } }
To log unhandled exceptions in .NET Framework, add this class:
public class NebolUnhandledExceptionLogger : ExceptionLogger { public override void Log(ExceptionLoggerContext context) { Logger.Instance.Error(context.Exception.ToString()); } }
and this line in WebApiConfig.Register():
config.Services.Replace(typeof(IExceptionLogger), new NebolUnhandledExceptionLogger());
and in .NET Core:
public class NebolErrorHandlingFilter : ExceptionFilterAttribute { public override void OnException(ExceptionContext context) { var exception = context.Exception; Logger.Instance.Error(context.Exception.ToString()); context.ExceptionHandled = true; //optional } }
then add the filter in Startup.cs ConfigureServices():
options.Filters.Add(new NebolErrorHandlingFilter());
OBSOLETE!
NebolLogger.EntityFramework.Core - for logging to database using .NET Core
OBSOLETE! .NET Core is now supported in NebolLogger.EntityFramework