我是这样做的:
private readonly Serilog.ILogger _logger; //= Log.ForContext( "Name", "Weather" );
public WeatherForecastController() {
string subPath = Path.Combine( DateTime.Now.ToString( "yyyy" ), DateTime.Now.ToString( "MM" ) ) + $"/{DateTime.Now.ToString("dd")}_Weather";
_logger = Log.ForContext( "Name", subPath );
}
.UseSerilog( ( hostingContext, loggerConfiguration ) => loggerConfiguration
.ReadFrom.Configuration( hostingContext.Configuration )
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.Map(
"Name",
"Request",
( name, wt ) => {
if (name == "Request")
wt.RollingFile( Path.Combine( $"{hostingContext.Configuration["LogPath"]}/{{Date}}-{name}.txt" ) );
else
wt.File( $"{hostingContext.Configuration["LogPath"]}/{name}.txt" );
} )
);