Giter Club home page Giter Club logo

logging-1's Introduction

NReco.Logging.File

Simple and efficient file logger provider for .NET Core (any version) without additional dependencies.

NuGet Release

  • very similar to standard ConsoleLogger but writes to a file
  • can append to existing file or overwrite log file on restart
  • supports a 'rolling file' behaviour and can control total log size
  • it is possible to change log file name on-the-fly
  • suitable for intensive concurrent usage: has internal message queue to avoid threads blocking

How to use

Add NReco.Logging.File package reference and initialize a file logging provider in services.AddLogging (Startup.cs):

services.AddLogging(loggingBuilder => {
	loggingBuilder.AddFile("app.log", append:true);
});

or

services.AddLogging(loggingBuilder => {
	var loggingSection = Configuration.GetSection("Logging");
	loggingBuilder.AddFile(loggingSection);
});

Example of the configuration section in appsettings.json:

"Logging": {
	"LogLevel": {
	  "Default": "Debug",
	  "System": "Information",
	  "Microsoft": "Error"
	},
	"File": {
		"Path": "app.log",
		"Append": "True",
		"MinLevel": "Warning",  // min level for the file logger
		"FileSizeLimitBytes": 0,  // use to activate rolling file behaviour
		"MaxRollingFiles": 0  // use to specify max number of log files
	}
}

Rolling File

This feature is activated with FileLoggerOptions properties: FileSizeLimitBytes and MaxRollingFiles. Lets assume that file logger is configured for "test.log":

  • if only FileSizeLimitBytes is specified file logger will create "test.log", "test1.log", "test2.log" etc
  • use MaxRollingFiles in addition to FileSizeLimitBytes to limit number of log files; for example, for value "3" file logger will create "test.log", "test1.log", "test2.log" and again "test.log", "test1.log" (old files will be overwritten).
  • if file name is changed in time (with FormatLogFileName handler) max number of files works only for the same file name. For example, if file name is based on date, MaxRollingFiles will limit number of log files only for the concrete date.

Change log file name on-the-fly

It is possible to specify a custom log file name formatter with FileLoggerOptions property FormatLogFileName. Log file name may change in time - for example, to create a new log file per day:

services.AddLogging(loggingBuilder => {
	loggingBuilder.AddFile("app_{0:yyyy}-{0:MM}-{0:dd}.log", fileLoggerOpts => {
		fileLoggerOpts.FormatLogFileName = fName => {
			return String.Format(fName, DateTime.UtcNow);
		};
	});
});

Note that this handler is called on every log message 'write'; you may cache the log file name calculation in your handler to avoid any potential overhead in case of high-load logger usage.

Custom log entry formatting

You can specify FileLoggerProvider.FormatLogEntry handler to customize log entry content. For example, it is possible to write log entry as JSON array:

loggerFactory.AddProvider(new NReco.Logging.File.FileLoggerProvider("logs/app.js", true) {
	FormatLogEntry = (msg) => {
		var sb = new System.Text.StringBuilder();
		StringWriter sw = new StringWriter(sb);
		var jsonWriter = new Newtonsoft.Json.JsonTextWriter(sw);
		jsonWriter.WriteStartArray();
		jsonWriter.WriteValue(DateTime.Now.ToString("o"));
		jsonWriter.WriteValue(msg.LogLevel.ToString());
		jsonWriter.WriteValue(msg.LogName);
		jsonWriter.WriteValue(msg.EventId.Id);
		jsonWriter.WriteValue(msg.Message);
		jsonWriter.WriteValue(msg.Exception?.ToString());
		jsonWriter.WriteEndArray();
		return sb.ToString();
	}
});

(in case of .NET Core 2 use loggingBuilder.AddProvider instead of loggerFactory.AddProvider).

License

Copyright 2017-2020 Vitaliy Fedorchenko and contributors

Distributed under the MIT license

logging-1's People

Contributors

lmoelleb avatar paulbol avatar vitaliymf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.