我的Startup
配置方法中有一个记录器:
public void Configure(
IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory ) {
loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
loggerFactory.AddDebug();
loggerFactory.AddFile(@"C:\Some\Path\portal-{Date}.txt");
我创建了一个实现IActionFilter.
的BenchmarkFilter类。启用后,它将在Action Methods上运行一些性能基准测试。我可以通过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
}
}
}