-
Notifications
You must be signed in to change notification settings - Fork 130
Closed
Description
logs fail to write when using configurelogger filter with the appsettings file shown below I have also included a file which is working.
public partial class App : Application
{
private void OnStartup(object sender, StartupEventArgs e)
{
IConfigurationBuilder builder = new ConfigurationBuilder();
BuildConfig(builder);
ILogger logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.CreateLogger();
logger.Information("Application Starting");
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddSingleton<MainWindow>();
services.AddSingleton<RootViewModel>();
services.AddSingleton(logger);
})
.UseSerilog()
.Build();
MainWindow mainWindow = ActivatorUtilities.CreateInstance<MainWindow>(host.Services);
mainWindow.DataContext = ActivatorUtilities.CreateInstance<RootViewModel>(host.Services);
mainWindow.Show();
}
private static void BuildConfig(IConfigurationBuilder builder)
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables();
}
}
}
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "(@Level = 'Error' or @Level = 'Fatal' or @Level = 'Warning')"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": ".\\Logs\\cp_.log",
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "(@Level = 'Information' or @Level = 'Debug')"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": ".\\Logs\\cp_.log",
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName"
],
"Properties": {
"Application": "MultipleLogFilesSample"
}
}
}
this appsettings.json works
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": ".\\Logs\\ex_.log",
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName"
],
"Properties": {
"Application": "MultipleLogFilesSample"
}
}
}
Metadata
Metadata
Assignees
Labels
No labels