记录 dotnet 核心 webapi 的扩展方法如何获取调用 projects/assembly 名称?
Extension method for logging dotnet core webapi how do I get the calling projects/assembly name?
这是扩展方法
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;
}
我们可以像这样在Startup
中使用它
app.UseLoggerConfig();
我想在 \%windir%\log\callingAppName\logfile.log
上保存日志。
有什么想法可以做到这一点吗?
我认为您可以查看 hosting environment 以获取此类信息。
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.
强调我的
鉴于这是要共享的,因此应该从它所在的位置显式注入它 used/called。即应用程序在 运行 中的 Web 托管环境。
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;
}
我们可以像这样在Startup
中使用它
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
//...code removed for brevity
app.UseLoggerConfig(env);
}
这也允许更改基于日志记录位置的环境类型,如开发、暂存、生产等。
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;
}
这是扩展方法
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;
}
我们可以像这样在Startup
中使用它
app.UseLoggerConfig();
我想在 \%windir%\log\callingAppName\logfile.log
上保存日志。
有什么想法可以做到这一点吗?
我认为您可以查看 hosting environment 以获取此类信息。
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.
强调我的
鉴于这是要共享的,因此应该从它所在的位置显式注入它 used/called。即应用程序在 运行 中的 Web 托管环境。
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;
}
我们可以像这样在Startup
中使用它
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
//...code removed for brevity
app.UseLoggerConfig(env);
}
这也允许更改基于日志记录位置的环境类型,如开发、暂存、生产等。
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;
}