简体   繁体   English

记录 dotnet core webapi 的扩展方法如何获取调用项目/程序集名称?

[英]Extension method for logging dotnet core webapi how do I get the calling projects/assembly name?

This is the extension method这是扩展方法

public static IApplicationBuilder UseLoggerConfig(this IApplicationBuilder app)
{
    //add serilog https://github.com/serilog/serilog-extensions-logging
    // https://github.com/serilog/serilog-extensions-logging/blob/dev/samples/Sample/Program.cs
    Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();

    var startTime = DateTimeOffset.UtcNow;

    Log.Logger.Information("Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);

    return app;
}

and we can use this in Startup like so我们可以像这样在Startup使用它

app.UseLoggerConfig();

I Would like to save the logs on \\\\%windir%\\log\\callingAppName\\logfile.log .我想将日志保存在\\\\%windir%\\log\\callingAppName\\logfile.log

Any ideas how we could do this ?任何想法我们怎么能做到这一点?

I am thinking you could check the hosting environment for information like that.我想您可以检查托管环境以获取此类信息。

IHostingEnvironment.ApplicationName

Gets or sets the name of the application.获取或设置应用程序的名称。 This property is automatically set by the host to the assembly containing the application entry point .该属性由宿主自动设置为包含应用程序入口点的程序集

emphasis mine强调我的

Given that this is meant to be shared, it should be explicitly injected from where it is being used/called.鉴于这是共享的,它应该从它被使用/调用的地方显式注入。 ie the web hosting environment the application is running in.即应用程序运行的网络托管环境。

public static IApplicationBuilder UseLoggerConfig(this IApplicationBuilder app, IHostingEnvironment env) {

    var callingAppName = env.ApplicationName;

    var path = $"{callingAppName}/logfile.log";

    //add serilog https://github.com/serilog/serilog-extensions-logging
    // https://github.com/serilog/serilog-extensions-logging/blob/dev/samples/Sample/Program.cs
    Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.File(path, rollingInterval: RollingInterval.Day)
    .CreateLogger();

    var startTime = DateTimeOffset.UtcNow;

    Log.Logger.Information("Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);

    return app;
}

and we can use this in Startup like so我们可以像这样在Startup使用它

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {

    //...code removed for brevity

    app.UseLoggerConfig(env);
}

This would also allow for the changing of logging location based environment type like development, staging, production, etc.这也将允许更改基于日志位置的环境类型,如开发、暂存、生产等。

public static IApplicationBuilder UseLoggerConfig(this IApplicationBuilder app, IHostingEnvironment env) {

    var callingAppName = env.ApplicationName;

    var path = $"{callingAppName}/logfile.log";
    if (env.IsDevelopment()) {
        // In Development logging path
    } else {
        // In Staging/Production logging path
    }

    //add serilog https://github.com/serilog/serilog-extensions-logging
    // https://github.com/serilog/serilog-extensions-logging/blob/dev/samples/Sample/Program.cs
    Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.File(path, rollingInterval: RollingInterval.Day)
    .CreateLogger();

    var startTime = DateTimeOffset.UtcNow;

    Log.Logger.Information("Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);

    return app;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM