diff --git a/docs/loggerFactory.md b/docs/loggerFactory.md index 3662516..5f52713 100644 --- a/docs/loggerFactory.md +++ b/docs/loggerFactory.md @@ -25,64 +25,64 @@ If you prefer to install the library manually, download the latest version from If you configure your logging in an XML file, simply add a reference to the Logz.io appender. ```xml - - + + - - <> + Required fields + --> + + <> - - - log4net - - https://<>:8071 - - - 100 - - 00:00:05 - - 3 - - 00:00:02 - - true - - false - - false + + + log4net + + https://<>:8071 + + + 100 + + 00:00:05 + + 3 + + 00:00:02 + + true + + false + + false - + ``` ### Code To add the Logz.io appender via code, add the following lines: ```C# - var hierarchy = (Hierarchy)LogManager.GetRepository(); - var logzioAppender = new LogzioAppender(); - logzioAppender.AddToken("<>"); - logzioAppender.AddListenerUrl("<>"); - // Uncomment and edit this line to enable proxy routing: - // logzioAppender.AddProxyAddress("http://your.proxy.com:port"); - // Uncomment these lines to enable gzip compression - // logzioAppender.AddGzip(true); - // logzioAppender.ActivateOptions(); - // logzioAppender.JsonKeysCamelCase(false) - logzioAppender.ActiveOptions(); - hierarchy.Root.AddAppender(logzioAppender); - hierarchy.Root.Level = Level.All; - hierarchy.Configured = true; +var hierarchy = (Hierarchy)LogManager.GetRepository(); +var logzioAppender = new LogzioAppender(); +logzioAppender.AddToken("<>"); +logzioAppender.AddListenerUrl("<>"); +// Uncomment and edit this line to enable proxy routing: +// logzioAppender.AddProxyAddress("http://your.proxy.com:port"); +// Uncomment these lines to enable gzip compression +// logzioAppender.AddGzip(true); +// logzioAppender.ActivateOptions(); +// logzioAppender.JsonKeysCamelCase(false) +logzioAppender.ActiveOptions(); +hierarchy.Root.AddAppender(logzioAppender); +hierarchy.Root.Level = Level.All; +hierarchy.Configured = true; ``` ## Custom Fields @@ -90,16 +90,16 @@ To add the Logz.io appender via code, add the following lines: You can add static keys and values to be added to all log messages. For example: ```XML - - - Environment - Production - - - Location - New Jerseay B1 - - + + + Environment + Production + + + Location + New Jerseay B1 + + ``` ## Extensibility @@ -107,14 +107,14 @@ You can add static keys and values to be added to all log messages. For example: If you want to change some of the fields or add some of your own, inherit the appender and override the `ExtendValues` method: ```C# - public class MyAppLogzioAppender : LogzioAppender +public class MyAppLogzioAppender : LogzioAppender +{ + protected override void ExtendValues(LoggingEvent loggingEvent, Dictionary values) { - protected override void ExtendValues(LoggingEvent loggingEvent, Dictionary values) - { - values["logger"] = "MyPrefix." + values["logger"]; - values["myAppClientId"] = new ClientIdProvider().Get(); - } + values["logger"] = "MyPrefix." + values["logger"]; + values["myAppClientId"] = new ClientIdProvider().Get(); } +} ``` You will then have to change your configuration in order to use your own appender. @@ -126,37 +126,36 @@ You will then have to change your configuration in order to use your own appende Update Startup.cs file in Configure method to include the Log4Net middleware as below. ```C# - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) - { - ... +public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) +{ + ... loggerFactory.AddLog4Net(); ... - } - +} ``` In the Controller add Data Member and Constructor as below. ```C# - private readonly ILoggerFactory _loggerFactory; +private readonly ILoggerFactory _loggerFactory; - public ExampleController(ILoggerFactory loggerFactory, ...) - { - _loggerFactory = loggerFactory; +public ExampleController(ILoggerFactory loggerFactory, ...) +{ + _loggerFactory = loggerFactory; - ... - } + ... +} ``` In the Controller methods: ```C# - [Route("")] - public ActionResult ExampleMethod() - { - var logger = _loggerFactory.CreateLogger(); +[Route("")] +public ActionResult ExampleMethod() +{ + var logger = _loggerFactory.CreateLogger(); var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); // Replace "App.config" with the config file that holds your log4net configuration @@ -168,38 +167,38 @@ In the Controller methods: LogManager.Shutdown(); return Ok(); - } +} ``` ### .NET Core Desktop Application ```C# - using System.IO; - using System.Reflection; - using log4net; - using log4net.Config; - using Microsoft.Extensions.Logging; - - namespace LoggerFactoryAppender - { - class Program +using System.IO; +using System.Reflection; +using log4net; +using log4net.Config; +using Microsoft.Extensions.Logging; + +namespace LoggerFactoryAppender +{ + class Program { - static void Main(string[] args) - { - ILoggerFactory loggerFactory = new LoggerFactory(); - loggerFactory.AddLog4Net(); + static void Main(string[] args) + { + ILoggerFactory loggerFactory = new LoggerFactory(); + loggerFactory.AddLog4Net(); - var logger = loggerFactory.CreateLogger(); - var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + var logger = loggerFactory.CreateLogger(); + var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); - // Replace "App.config" with the config file that holds your log4net configuration - XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); + // Replace "App.config" with the config file that holds your log4net configuration + XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); - logger.LogInformation("Hello"); - logger.LogInformation("Is it me you looking for?"); + logger.LogInformation("Hello"); + logger.LogInformation("Is it me you looking for?"); - LogManager.Shutdown(); - } + LogManager.Shutdown(); + } } - } +} ```