the application uses Microsoft.Extensions.Logging
_logger.LogError
_logger.LogInformation
this is configured in appsettings.json
"Logging": {
"IncludeScopes": false,
"Console": {
"LogLevel": {
"Default": "Information"
}
}
},
then I am using mountebank as config server.
at imposter file
"propertySources": [
{
"source": {
"Logging.Console.LogLevel.Default": "Information",
}
How do I enable all log to include error as well?
Related
I run the sample for NET Core:
[https://github.com/mattwcole/gelf-extensions-logging/blob/dev/samples/Gelf.Extensions.Logging.Samples.NetCore2/Program.cs][1]
Then I have ran Docker
https://docs.graylog.org/en/4.0/pages/installation/docker.html?highlight=docker
Then I little bit corrected appsettings.json file:
{
"Logging": {
"Console": {
"LogLevel": {
"Default": "Debug"
}
},
"GELF": {
"Host": "127.0.0.1",
"Port": 12201,
"LogSource": "console-app-1",
"LogLevel": {
"Default": "Debug",
"Microsoft": "Debug",
"Gelf.Extensions.Logging.Samples.NetCore2": "Debug",
"Gelf.Extensions.Logging.Samples.NetCore2.Program": "Debug"
},
"LogLevel2": {
"Microsoft.AspNetCore.Mvc.Razor": "Error",
"Default": "Trace"
},
"AdditionalFields": {
"project_name": "my-project"
}
}
}
}
Run the application, it works but when I go to browser
http://localhost:9000/search?q=&rangetype=relative&relative=1800
I do not see any records.
What I do wrong?
Need to check the Docker GrayLog Configuration: especially GELF Input local and global.
I'm just starting with Serilog.
Despite all the code samples/tut's, I've found online I just can't get it to output to file (the file isn't even created). My app is a Web API (.NET Core 3.1), I'm referencing
Serilog.AspNetCore(3.4.0)
Serilog.Settings.Configurations (3.1.0)
Serilog.Sinks.File (4.1.0)
My appsettings.json:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"System": "Warning"
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\DEV\\Logs\\mylog.txt",
"rollingInterval": "Day"
}
}
]
}
},
"AllowedHosts": "*"
}
My Program.cs
public static void Main(string[] args)
{
Serilog.Debugging.SelfLog.Enable(Console.Out);
//Read Configuration from appSettings
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
//Initialize Logger
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger();
try
{
CreateHostBuilder(args).Build().Run();
Log.Information("Application started!");
}
catch (Exception e)
{
Log.Fatal(e, "Application failed to start.");
}
finally
{
Log.CloseAndFlush();
}
}
My controller
public void Post(SampleRequest request)
{
Log.Information("Received request {#request}", request);
}
Not even the Selflog is writing anything to Visual Studio output console Serilog.Debugging.SelfLog.Enable(Console.Out);
Try and enable SelfLog which should help you pinpoint what is going wrong. This call is slightly different to yours.
Add this in Program.cs just after you call .CreateLogger();
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
More details - https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics
I have Serilog logging to a rolling file in a .Net Core 3.1 app.
These are my nuget references:
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
Also, just noticed you don't seem to have a Using section in your appsettings.json:
"Serilog": {
"Using": [ "Serilog.Sinks.RollingFile" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "C:\\Logs\\ScreenPop\\Log-{Date}.txt"
}
}
]
},
I figured out what was the problem. I copied settings from some blog and the "WriteTo" element was actually nested inside the "MinimumLevel" one.
The correct settings would be:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\DEV\\Logs\\mylog-.txt",
"rollingInterval": "Day"
}
}
]
},
"AllowedHosts": "*"
}
I have an app that configures Serilog via appsettings.json file, i develop the app on windows but it is deployed to linux.
How can i write logs to a relative directory Logs/logfile.log?
Is just changing the appsettings.json at deployment folder a good option?
right now this is the config i am using:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.MSSQLServer" ],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"MinimumLevel": {
"Default": "Warning",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {SourceContext} [{Level}] {Message}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Grayscale, Serilog.Sinks.Console"
}
},
{
"Name": "File",
"Args": {
"path": "Logs\\log-.log",
"rollingInterval": "Day",
"retainedFileCountLimit": 5
}
}
]
},
What you can do is the following:
Create two appsettings files one should be the default and the second one appsettings.Linux.json
Read the appsettings file depending on which OS the application is running.
var env = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ?
"Linux" : "Windows";
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env}.json", true)
.AddEnvironmentVariables()
.Build();
Set different values for the logger in the appsettings.Linux.json.
For Windows:
"Args": {
"path": "Logs\log-.log",
"rollingInterval": "Day",
"retainedFileCountLimit": 5
}
For Linux:
"Args": {
"path": "/var/log/appname",
"rollingInterval": "Day",
"retainedFileCountLimit": 5
}
I'm trying to follow a tutorial on adding an Azure Cognitive Search service to my .NET Core app, and don't want to mess anything up. The tutorial says to add the following to my appsettings.json file:
{
"SearchServiceName": "<placeholder-Azure-Search-service-name>",
"SearchServiceAdminApiKey": "<placeholder-admin-key-for-Azure-Search>",
"AzureSqlConnectionString": "<placeholder-ADO.NET-connection-string",
}
Problem is, my appsettings.json file already has stuff in it:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=xxx.database.windows.net,1433;Initial Catalog=XChange;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
When I try to add the Azure API call at the bottom it says only one top-level item is allowed, and if I assign some key to it inside the top-level json object I'm afraid the API won't work -- I don't use JSON very much and I'm new to .NET so sorry if this is a stupid question, I couldn't find any docs explaining what to do
It means your JSON is incorrect , you need to have it in the form of object with a key. Do something like
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=xxx.database.windows.net,1433;Initial Catalog=XChange;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Configuration": {
"SearchServiceName": "<placeholder-Azure-Search-service-name>",
"SearchServiceAdminApiKey": "<placeholder-admin-key-for-Azure-Search>",
"AzureSqlConnectionString": "<placeholder-ADO.NET-connection-string"
}
}
When I attempt to use custom appsettings.{environment}.json file, even though the ASPNETCORE_ENVIRONMENT environment var is correctly set to "Local" and the appsettings.Local.json file is referenced in the config sources, no data is read from it, only from appsettings.json
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
// Configure the app here.
var env = context.HostingEnvironment;
//var environmentName =
config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
})
.UseStartup<Startup>();
Showing values from appsettings.json
Showing config values from appsettings.Local.json (empty)
------ appsettings.json ----------------------
{
"AllowedHosts": "*",
"AppSettings": {
"MailServer": "mail.somewhare.com",
"SupportContact": "somebody#somewhare.com",
"EmailEnabled": true
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
------ appsettings.Local.json ---------------
{
"ConnectionStrings": {
"Dbconn1": "string 1 here",
"DbConn2": "string 1 here"
},
"AllowedHosts": "*",
"AppSettings": {
"CustomerBoxRoot": "\\\\test1\\d$\\BoxFiles",
"AnotherAppSetting": 20,
"PurgeSwitch": -14
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}