我的 Startup
配置方法中有一个 Logger :
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传递这个 Logger 但是我需要这个以csv格式登录到新的文件位置 . 我怎么会这样呢?我的 class 到目前为止:
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
}
}
}