How to use Log4Net in .Net Core 2.0 Application.
1. Introduction
Logging frameworks are important because they make it easy to write your logs to different places by simply changing your configuration. You can write your .NET logs to a file on disk, a database, a log management system
2. Install Nuget Package For nlog
To use Nlog logging you need to first add Nlog pluging for adding pluging you can do it by different way.
1) Manage Nuget Packages.
2) Nuget command.
you can find nuget command for Log4Net below.
PM> Install-Package log4net -Version 2.0.8
3. Add log4net.config file
We need to Add New to your project with name log4net.config
Refer below code for log4net.config for logging log into file.
Root is neccesary in log4net.config, in which we can define log level and appender-ref in which we can define appender for
eg. FileAppender, ConsoleAppender.
Layout : In layout we can define custom parameter shown below.
4. Logging Levels
There are seven logging levels five.
a) OFF - nothing gets logged (cannot be called)
b) FATAL
c) ERROR
d) WARN
e) INFO
f) DEBUG
g) ALL - everything gets logged (cannot be called)
5. Different Appender
a) Rolling File Appender -It writes to the output window, or the command window.
b) File Appender -This appender will write to a text file.
c) ADO.NET Appender -This appender will write to a Database.
d) Console Appender
-This appender Perform the same functions as the file appender but with the additional option to only store a certain amount of data before starting a new log file
6. Logging Manager of Log4Net
public static class Logger
{
private static readonly string LOG_CONFIG_FILE = @"log4net.config";
private static readonly log4net.ILog _log = GetLogger(typeof(Logger));
public static ILog GetLogger(Type type)
{
return LogManager.GetLogger(type);
}
public static void Debug(object message)
{
SetLog4NetConfiguration();
_log.Debug(message);
}
private static void SetLog4NetConfiguration()
{
XmlDocument log4netConfig = new XmlDocument();
log4netConfig.Load(File.OpenRead(LOG_CONFIG_FILE));
var repo = LogManager.CreateRepository(
Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);
}
}
Load and Read Log4Net Config File:
You need to name your config file to be your assembly name and it needs to have the extension you specify. Here is an example
private static void SetLog4NetConfiguration()
{
XmlDocument log4netConfig = new XmlDocument();
log4netConfig.Load(File.OpenRead(LOG_CONFIG_FILE));
var repo = LogManager.CreateRepository(
Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);
}
Happy Coding. Please Add comment if you have any query.
I cannot see code in 3 of the 6 snippets. Might just be me
ReplyDeletePlease check now it is visible
DeleteI followed all your steps but not able to find log file. Where is it created? Thanks.
ReplyDeleteI also cant find the file :(
Delete