dependency-injection – 从ILogger访问当前的HttpContext
发布时间:2020-12-15 21:00:11 所属栏目:asp.Net 来源:网络整理
导读:在ASP.NET Core 1.0中,我有一个ILoggerProvider和ILogger接口的自定义实现.我希望能够从Log方法访问HttpContext. 我似乎需要将IHttpContextAccessor注入自定义ILogger,但无法找到如何做到这一点. ILoggerProvider对象在启动时创建,CreateLogger方法不允许依
在ASP.NET Core 1.0中,我有一个ILoggerProvider和ILogger接口的自定义实现.我希望能够从Log方法访问HttpContext.
我似乎需要将IHttpContextAccessor注入自定义ILogger,但无法找到如何做到这一点. ILoggerProvider对象在启动时创建,CreateLogger方法不允许依赖注入. 是否有一种简单的方法可以使用依赖注入ILogger? 解决方法
这是一个例子
Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IHttpContextAccessor,HttpContextAccessor>(); } public void Configure(IApplicationBuilder app,ILoggerFactory loggerFactory,IServiceProvider serviceProvider) { loggerFactory.AddCustomLogger(serviceProvider.GetService<IHttpContextAccessor>()); // } 自定义记录器: public class CustomLogProvider : ILoggerProvider { private readonly Func<string,LogLevel,bool> _filter; private readonly IHttpContextAccessor _accessor; public CustomLogProvider(Func<string,bool> filter,IHttpContextAccessor accessor) { _filter = filter; _accessor = accessor; } public ILogger CreateLogger(string categoryName) { return new CustomLogger(categoryName,_filter,_accessor); } public void Dispose() { } } public class CustomLogger : ILogger { private string _categoryName; private Func<string,bool> _filter; private readonly IHttpContextAccessor _accessor; public CustomLogger(string categoryName,Func<string,IHttpContextAccessor accessor) { _categoryName = categoryName; _filter = filter; _accessor = accessor; } public IDisposable BeginScope<TState>(TState state) { return null; } public bool IsEnabled(LogLevel logLevel) { return (_filter == null || _filter(_categoryName,logLevel)); } public void Log<TState>(LogLevel logLevel,EventId eventId,TState state,Exception exception,Func<TState,Exception,string> formatter) { if (!IsEnabled(logLevel)) { return; } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } var message = formatter(state,exception); if (string.IsNullOrEmpty(message)) { return; } message = $"{ logLevel }: {message}"; if (exception != null) { message += Environment.NewLine + Environment.NewLine + exception.ToString(); } if(_accessor.HttpContext != null) // you should check HttpContext { message += Environment.NewLine + Environment.NewLine + _accessor.HttpContext.Request.Path; } // your implementation } } public static class CustomLoggerExtensions { public static ILoggerFactory AddCustomLogger(this ILoggerFactory factory,IHttpContextAccessor accessor,bool> filter = null) { factory.AddProvider(new CustomLogProvider(filter,accessor)); return factory; } } 虽然以上方式有效,但我更愿意实现自定义IRequestLogger而不是注入HttpContextAccessor.实现如下(未经测试): public interface IRequestLogger<T> { void Log(LogLevel logLevel,string message); // you can change this } public class RequestLogger<T> : IRequestLogger<T> { private readonly IHttpContextAccessor _accessor; private readonly ILogger _logger; public RequestLogger(ILogger<T> logger,IHttpContextAccessor accessor) { _accessor = accessor; _logger = logger; } public void Log(LogLevel logLevel,string message) { Func<object,string> _messageFormatter = (object state,Exception error) => { return state.ToString(); }; _logger.Log(LogLevel.Critical,new FormattedLogValues(message),null,_messageFormatter); } } 简单用法: public class LogType { } public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IHttpContextAccessor,HttpContextAccessor>(); services.AddSingleton(typeof(IRequestLogger<>),typeof(RequestLogger<>)); } public void Configure(IApplicationBuilder app,ILoggerFactory loggerFactory) { loggerFactory.AddConsole(true); app.Run(async (context) => { var requestLogger = context.RequestServices.GetService<IRequestLogger<LogType>>(); requestLogger.Log(LogLevel.Critical,11,"<message>"); // }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-core – 为Microsoft.CodeAnalysis.Common检测到版
- 如何在ASP.NET Core自定义中间件中读取Request.Body和Respo
- asp.net-mvc – 如何查看mvc应用程序中的所有视图
- ASP.NET:获取页面的文件名
- asp.net-mvc-2 – 使用Ninject返回null的HttpHandler属性注
- asp.net-mvc – 如何使用Html.EditorFor与MVC3设置id
- asp.net-mvc – Web Api调用返回404错误,GUID作为参数传递
- asp.net-mvc – ASP.Net MVC风格包不包括大多数文件
- [译]ASP.NET Core 2.0 网址重定向的方法
- asp.net-mvc-4 – Asp.Net Web Api – ModelBinders
推荐文章
站长推荐
- 在ASP.NET Core上实施每个租户策略的数据库
- asp.net – 处理程序执行子请求时出错’System.W
- asp.net – 任何类似于蚂蚁分析器和免费的工具?
- asp.net-mvc – ASP.Net Core在调试模式下运行时
- asp.net-mvc – 在AJAX请求之前单击按钮时的客户
- asp.net – 使用CRC的较短GUID
- asp.net – 无法打开登录请求的数据库.登录失败.
- asp.net – WebControl vs HtmlControl. Cos和专
- asp.net-mvc – IoC Castle Windsor – 没有为此
- asp.net-mvc-4 – Dapper多对多查询
热点阅读