C
C#17mo ago
PontiacGTX

✅ Serilog does not write logs to file

I have tried to configure it on the appsettings to write file but it doesnt
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Debug",
"Microsoft": "Information",
"System": "Information"
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}"
},
{

"Name": "File",
"Args": {

"pathFormat": "\\Employeeslog-{Date}.txt",
"rollOnFileSizeLimit": true,
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
"formatter": "Serilog.Formatting.Json.JsonFormatter",
"rollingInterval": "Day"
}
}
]
}
}
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Debug",
"Microsoft": "Information",
"System": "Information"
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}"
},
{

"Name": "File",
"Args": {

"pathFormat": "\\Employeeslog-{Date}.txt",
"rollOnFileSizeLimit": true,
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
"formatter": "Serilog.Formatting.Json.JsonFormatter",
"rollingInterval": "Day"
}
}
]
}
}
75 Replies
PontiacGTX
PontiacGTXOP17mo ago
and the included packages are
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.6.5" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.6.5" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
Saber
Saber17mo ago
how are you configuring serilog in the code
Omnissiah
Omnissiah17mo ago
at first sight config seems alright except maybe pathFormat which i don't know where the hell that would point at like maybe "folder\file.txt" could be ok, but starting with \ is probably not even .\file.txt should be ok
PontiacGTX
PontiacGTXOP17mo ago
I am using
builder.Host.UseSerilog((context, configuration) => {
configuration.ReadFrom.Configuration(context.Configuration); });

builder.AddLoggers();

//after creating the app
//Add serilog to the pipeline
app.UseSerilogRequestLogging();
builder.Host.UseSerilog((context, configuration) => {
configuration.ReadFrom.Configuration(context.Configuration); });

builder.AddLoggers();

//after creating the app
//Add serilog to the pipeline
app.UseSerilogRequestLogging();
will it create the folder if it doesnt exist? because the folder does not exist the console works
Omnissiah
Omnissiah17mo ago
in my experience it does create the folder
PontiacGTX
PontiacGTXOP17mo ago
"pathFormat": "./Employeeslog-{Date}.txt", like that?
Omnissiah
Omnissiah17mo ago
is this win, linux, osx?
PontiacGTX
PontiacGTXOP17mo ago
it will be on linux probably my pc is windows that's why using a env variable would be cool how do you replace a env variable in serilog %varname% or ${varname}
Omnissiah
Omnissiah17mo ago
apparently it's %var% although i've never used that
PontiacGTX
PontiacGTXOP17mo ago
so %var%/mypath/myfile.txt what am i doing wrong?
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Debug",
"Microsoft": "Information",
"System": "Information"
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}"
},
{

"Name": "File",
"Args": {

"pathFormat": ".\\Employeeslog-{Date}.txt",
"rollOnFileSizeLimit": true,
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
"formatter": "Serilog.Formatting.Json.JsonFormatter",
"rollingInterval": "Day"
}
}
]
}
}
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Debug",
"Microsoft": "Information",
"System": "Information"
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}"
},
{

"Name": "File",
"Args": {

"pathFormat": ".\\Employeeslog-{Date}.txt",
"rollOnFileSizeLimit": true,
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
"formatter": "Serilog.Formatting.Json.JsonFormatter",
"rollingInterval": "Day"
}
}
]
}
}
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
i will do that instead then
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
that is for debugging only
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
they are meant ot include it in the configuration
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
these using probably to include it in the WriteTo
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
why?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
I referenced
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.6.5" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.6.5" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
it is the damn path thign or the fomratting
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
they had done it before
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
I dont konw why they changed their ways
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
now you us ea Static class
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
i dont know why they changed that because in 4.0 it worked with microsoft logger
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
Log is their own
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
mm i am using it to ensure it is reading the log from my string
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
so I dont get that my code is not calling serilog method for no reason
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
how?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX17mo ago
TeBeCo
.Ovverride(new {
["Default"] = "Debug",
["Microsoft"] = "Information",
["System"] = "Information",
}
.WriteTo.File(fileSinkOptions)
.WriteTo.Console(consoleSinkOptions)
.FromLogContext()
)
.Ovverride(new {
["Default"] = "Debug",
["Microsoft"] = "Information",
["System"] = "Information",
}
.WriteTo.File(fileSinkOptions)
.WriteTo.Console(consoleSinkOptions)
.FromLogContext()
)
Quoted by
React with ❌ to remove this embed.
PontiacGTX
PontiacGTXOP17mo ago
makes sense
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
those are other sinks i dont think they belong to Serilog
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
ops yeah but they are their own package I mean
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
yes i know i forgot the terminology
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
@TeBeClone one question can i override the reader form serilog so I can see what it isreading for the path? or know what is reading for the file path
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
maybe it is not recognizing the path i mean the one that reads the configuration from appsettings so I can see what it is receiving
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
well this is like a blackbox with the json
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
it is within the container
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
so there are like 300 services
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
lol
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
so you telll me every logger that is created is replaced by theirs at the IloggerFactory they impelement i didnt knwo that good think i only do ILogger DI with generic and I dont use the ILoggerProvider/Factory
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
as far the DI works fine I am fine I dont plan using a LoggerFactory if it was some other dependency package that required one I Would be worried but I am fine using whatever they inject lol
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
what kind of enricher do you need to make one your own in what case scenario do you need one?
PontiacGTX
PontiacGTXOP17mo ago
Stack Overflow
Serilog - how to customize date in rolling file name?
In Serilog, you can easily enable rolling log files: Log.Logger = new LoggerConfiguration() .WriteTo.File("log-.txt", rollingInterval: RollingInterval.Day) .CreateLogger(); This will crea...
PontiacGTX
PontiacGTXOP17mo ago
how do I place the Date on the file
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
builder.Host.UseSerilog((context, configuration) => {

//configuration.ReadFrom.Configuration(context.Configuration);
configuration.WriteTo
.Console()
.MinimumLevel
.Information();
var fileName = "employeesLog-{Date}";
fileName = string.Concat(AppDomain.CurrentDomain.BaseDirectory, fileName, ".txt");


Log.Logger = configuration.WriteTo.File(fileName, rollingInterval: RollingInterval.Day).CreateLogger();
});
builder.Host.UseSerilog((context, configuration) => {

//configuration.ReadFrom.Configuration(context.Configuration);
configuration.WriteTo
.Console()
.MinimumLevel
.Information();
var fileName = "employeesLog-{Date}";
fileName = string.Concat(AppDomain.CurrentDomain.BaseDirectory, fileName, ".txt");


Log.Logger = configuration.WriteTo.File(fileName, rollingInterval: RollingInterval.Day).CreateLogger();
});
like this? makes sense
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
how do i assign the property into the config for serilog then?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
7
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTXOP17mo ago
ok let me join
Want results from more Discord servers?
Add your server