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"); } }