Asp.net mvc 如何创建第二个日志记录器,将日志记录到ASP.NETCore中的文件?
我的Asp.net mvc 如何创建第二个日志记录器,将日志记录到ASP.NETCore中的文件?,asp.net-mvc,logging,asp.net-core,dependency-injection,Asp.net Mvc,Logging,Asp.net Core,Dependency Injection,我的启动配置方法中有一个记录器: public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory ) { loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) ); loggerFactory.AddDebug(); loggerFactory.AddF
启动配置方法中有一个记录器:
public void Configure(
IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory ) {
loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
loggerFactory.AddDebug();
loggerFactory.AddFile(@"C:\Some\Path\portal-{Date}.txt");
我创建了一个BenchmarkFilter类,它实现了IActionFilter。
启用后,它将在操作方法上运行一些性能基准。我可以通过DI将其传递给记录器,但我需要它以csv格式记录到新的文件位置。我该怎么办?我的班级到目前为止:
public class BenchmarkFilter : IActionFilter
{
private readonly ILogger _logger;
private readonly bool _isBenchmarkOn;
private string _benchmarkFilePath;
private Stopwatch _stopWatch = new Stopwatch();
public BenchmarkFilter(
ILoggerFactory loggerFactory, IOptions<AppSettings> appSettings)
{
_isBenchmarkOn = appSettings.Value.EnableBenchmarkLogging;
_benchmarkFilePath = appSettings.Value.BenchmarkFilePath;
}
public void OnActionExecuting(ActionExecutingContext context)
{
if (_isBenchmarkOn)
{
_stopWatch = Stopwatch.StartNew();
}
}
public void OnActionExecuted(ActionExecutedContext context)
{
if (_stopWatch.IsRunning)
{
_stopWatch.Stop();
var seconds = _stopWatch.ElapsedMilliseconds;
//logging to do
}
}
}
公共类基准过滤器:IActionFilter
{
专用只读ILogger\u记录器;
私有只读bool_isBenchmarkOn;
私有字符串_benchmarkFilePath;
私人秒表_Stopwatch=新秒表();
公共基准过滤器(
iLogger工厂日志工厂,IOptions应用设置)
{
_isBenchmarkOn=appSettings.Value.EnableBenchmarkLogging;
_benchmarkFilePath=appSettings.Value.benchmarkFilePath;
}
ActionExecuting(ActionExecutingContext上下文)上的公共void
{
如果(_isBenchmarkOn)
{
_stopWatch=stopWatch.StartNew();
}
}
public void OnActionExecuted(ActionExecutedContext上下文)
{
如果(_秒表正在运行)
{
_秒表;
var seconds=_stopWatch.elapsedmillisonds;
//日志记录要做什么
}
}
}
您是否了解了功能。似乎您可以设置规则以将授权筛选器日志输出到特定的提供程序。我不太清楚如何使用该规则将不同的记录器(到文件)传递到BenchMarkFilter,我应该能够吗?实际上您不会传递不同的记录器,其思想是创建和配置多个日志提供程序,其中一个是特殊的CSV版本。然后,使用一个过滤器配置CSV日志,该过滤器仅侦听自定义授权过滤器消息,并仅记录这些消息。