首页 > 其他 > 详细

Serilog with Autofac

时间:2015-10-31 13:00:06      阅读:277      评论:0      收藏:0      [点我收藏+]

http://serilog.net/  ---不错的日志工具

 

1.直接绑定

 

builder.Register<ILogger>((c, p) =>
{
    return new LoggerConfiguration()
      .WriteTo.RollingFile(
        AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/Log-{Date}.txt")
      .CreateLogger();
}).SingleInstance();
http://docs.autofac.org/en/latest/register/registration.html#selection-of-an-implementation-by-parameter-value
2.使用 AutofacSerilogIntegration
nuget安装 Install-Package AutofacSerilogIntegration
 
static void Main()
        {
            Log.Logger = new LoggerConfiguration()
                .Enrich.WithProperty("SourceContext", null)
                .WriteTo.LiterateConsole(
                    outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {Message} ({SourceContext:l}){NewLine}{Exception}")
                .CreateLogger();

            try
            {
                var builder = new ContainerBuilder();
                builder.RegisterLogger(autowireProperties: true);
                builder.RegisterType<AcceptsLogViaCtor>().As<IExample>();
                builder.RegisterType<AcceptsLogViaProperty>().As<IExample>();

                using (var container = builder.Build())
                {
                    var examples = container.Resolve<IEnumerable<IExample>>();
                    foreach (var example in examples)
                    {
                        example.Show();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Unhandled error");
            }
        }

Serilog with Autofac

原文:http://www.cnblogs.com/miralce/p/4925159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!