dependency-injection – 从ILogger访问当前的HttpContext
发布时间:2020-08-16 08:29:38 所属栏目: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 – WebForm_DoCallback定义
- asp.net-mvc – ASP.NET MVC中的Windows Live ID
- 谈基于.net平台开发中的模式窗体
- asp.net-mvc – 在IIS 5.1上部署ASP.NET MVC(Windows XP)
- 在ASP.NET中拒绝用户时,’CustomIdentity’上的Serializati
- jQuery validate 根据 asp.net MVC的验证提取简单快捷的验证
- asp.net-mvc-4 – AngularJs,DropZone.Js,MVC4 – 拖放,预览
- 敏感词汇过滤DFA算法
- asp.net-mvc – 当我不知道内容类型时如何返回文件结果
- 如何保护我的ASP.NET AJAX应用程序?
推荐文章
站长推荐
- asp.net-mvc – ASP .Net MVC 3:子动作和重定向
- asp.net – Ajax Control Toolkit正在加载太多脚
- asp.net-mvc – 在ASP.NET MVC中实现工作单元的方
- 动态Linq的逻辑与和逻辑或的条件查询
- asp.net – 多线程环境中的文件访问策略(Web App
- asp.net – 通过Web服务访问连接字符串
- asp.net-mvc – 如何从剃刀视图访问My.Resources
- asp.net-mvc – Url.Action生成查询字符串,以任何
- ASP.NET MVC Url路由支持(点)
- asp.net-mvc-2 – 如何使用Castle Windsor在MVC中
热点阅读
