madaxe2020
Well-known member
- Joined
- Sep 7, 2020
- Messages
- 50
- Programming Experience
- 5-10
Evening All,
My logs wont write to file, if I uncomment '.WriteTo.File("log.txt")' then I get a log file, but I really want the appsettings to manage the configration. Can anybody see why its not writting the log files
Thanks in Advance
Madaxe
My logs wont write to file, if I uncomment '.WriteTo.File("log.txt")' then I get a log file, but I really want the appsettings to manage the configration. Can anybody see why its not writting the log files
Thanks in Advance
Madaxe
App.XAML.cs:
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()
//.WriteTo.Console()
//.WriteTo.File("log.txt")
.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();
}
}
}
RootViewModel:
public interface IRootViewModel
{
}
public class RootViewModel
{
private ILogger _logger;
public RootViewModel(ILogger logger)
{
_logger = logger;
_logger.Information("Writing to log file with INFORMATION severity level.");
_logger.Debug("Writing to log file with DEBUG severity level.");
_logger.Warning("Writing to log file with WARNING severity level.");
_logger.Error("Writing to log file with ERROR severity level.");
_logger.Fatal("Writing to log file with CRITICAL severity level.");
}
}
appsettings.json:
{
"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"
}
}
}