Skip to content

EsWork/Es.Logging

Repository files navigation

Es.Logging

Build Status

Es.Logging抽象出.Net平台的日志通用接口,自身并没有日志处理实现,默认实现了NLog、Log4net、Microsoft.Extensions.Logging和console standard output支持。

通常日志的实例我们都在构造函数中创建

public class Foo{
    private readonly ILogger _logger;

    public Foo(){
       _logger = LoggerManager.GetLogger<Foo>();
    }
}

但是很多时候我们也许会在静态类使用,也许Logger静态实例在没有LoggerProvider时候可能会失效,Es.Logging现在已经支持。

public static class Foo{
    private static readonly ILogger _logger = LoggerManager.GetLogger<Foo>();
}

Features

  • 聚合.NET平台常用日志类库
  • 已有静态Logger实例支持后期追加的LoggerProvider
  • 支持Unix&Windows控制台输出
  • 支持NLog
  • 支持Log4net
  • 支持Serilog
  • 支持Microsoft.Extensions.Logging

Packages & Status

Package NuGet
Es.Logging NuGet package
Es.Logging.Console NuGet package
Es.Logging.NLog NuGNuGet packageet
Es.Logging.Log4 NuGet package
Es.Logging.Serilog NuGet package
Es.Microsoft.Logging NuGet package

Usage

标准控制台输出

using Es.Logging;

public static class Foo{
    //static Logger instance
    private static readonly ILogger _logger = LoggerManager.GetLogger<Foo>();

     private static void Main(string[] args) {
         //add console output
         LoggerManager.Factory.AddConsole(LogLevel.Trace);
         //logger info
         _logger.Info("Es.Logging");
     }
}

推荐使用NLog

using Es.Logging;

public static class Foo{
    //static Logger instance
    private static readonly ILogger _logger = LoggerManager.GetLogger<Foo>();

     private static void Main(string[] args) {
          //init NLog config
          LoggingConfiguration config = new LoggingConfiguration();
          ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
          config.AddTarget("console", consoleTarget);
          LoggingRule rule = new LoggingRule("*", NLog.LogLevel.Trace, consoleTarget);
          config.LoggingRules.Add(rule);

          LogFactory factory = new LogFactory(config);

          //add NLog Provider
          _logFactory.AddNLog(factory);

          //logger info
          _logger.Info("Es.Logging");
     }
}

更多请查看Sample

License

See LICENSE for details.