I have a application with a Log4net RollingFileAppender for info-logs and a EventLog for error-logs
builder.ClearProviders()
.AddEventLog(new EventLogSettings { SourceName = "ABC"})
.AddLog4Net();
Configured in appsettings here
"Logging": {
"EventLog": {
"LogLevel": {
"Default": "Error"
}
},
"LogLevel": {
"Default": "None",
"MyNamespace.*": "Information"
}
}
When create a WebApplicationFactory in my test suite, I substitute the logger to an in-memory implantation with this code
builder.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddProvider(_inMemLoggerProvider);
});
The InMemLoggerProvider is inspired by this code https://github.com/horsdal/InMemoryLogger
When running the test-suite all errors logged from MyNamespace is caught by the InMemoryLogger.
But errors logged from other namespaces is not caught by the InMemoryLogger.
When running the application without substituting the logger, errors are logged fine to the Event Log.
Related
I've created a C# .net5.0 console application and during testing Serilog has been working without incident, logging to Console and File (same folder; path="log.txt"). However, when I run on the application on our server, neither Console nor File logging sinks are working! I assume now that the issue is not the sinks themselves but Serilog not actually working.
I've tried enabling the self log:
Serilog.Debugging.SelfLog.Enable(msg =>
Console.WriteLine(msg)
);
but even running in the debugger in my dev environment, the Console.WriteLine(msg) line is never called!
My appsettings.json is as follows:
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"System": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Infinite",
"outputTemplate": "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"shared": false
}
}
],
"Enrich": [ "FromLogContext" ]
},
"Database": {
"Server": "(local)",
"Database": "ActivbaseLive"
},
"Email": {
"SmtpHost": "localhost",
"SmtpPort": 25,
"SmtpSecurityOption": "None",
"SmtpUsername": null,
"SmtpPassword": null,
"SmtpSender": "\"Activbase Learning Platform\" <noreply#activbase.net>"
}
}
I've tried absolute paths (using double backslashes in appsettings.json).
I've tried pre-creating the log file (e.g. log.txt and log200428.txt) and setting permissions to Everyone Full Control but neither of these changes fix the problem and they don't explain why the Console sink doesn't write either.
Here is how Serilog is being configured during start-up which is where I suspect the problem is (even through it works in dev environment):
return Host.CreateDefaultBuilder()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
})
.UseSerilog((hostContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(hostContext.Configuration);
})
.ConfigureAppConfiguration((hostContext, builder) =>
{
builder.AddEnvironmentVariables();
})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
...
});
}
Any ideas why Serilog isn't working in production?
The Path you provide should be absolute.
Some thing like this:
"path": "E:/wwwroot/QA/BackgroundWorkerService/Logs/logFile_.log"
Even I had the same issue, the above fix worked fine for me...
For my api application running in IIS: I had to assign the following permissions to the log folder for the IIS_IUSRS. I didn't need an absolute path!
I am confused about the purpose Using in this configuration.
Unfortunately the keyword "using" is a very common word, so googling gave no usable result:
a) When configuring with code, I see no analogous builder method in the samples, just the .WriteTo() methods
b) It is interesting, I even commented it out, still everything works (both the console, both the file).
Are not the two Name elements under the WriteTo element (namely Console and File) are the two sinks here? Maybe the Using is a namespace to look for the classes? (then why it works when commented out? and also this should mean that both Console sink and File sink is under the namespace Serilog.Sinks.Console)
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Debug",
"System": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": "%TEMP%\\Logs\\mylog.txt"
//"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] [Req: {RequestInfo}] {Message}{NewLine}{Exception}"
}
}
]
}
I'm trying to implement a lambda function with an iOS app. I follow all the steps on this tutorial form AWS: https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-ios-lambda.html.
But when I add the following line:
let lambdaInvoker = AWSLambdaInvoker.default()
it throws this error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The service configuration is `nil`. You need to configure `Info.plist` or set `defaultServiceConfiguration` before using this method.'
I added the awsconfiguration.json file to the project with this content:
{
"Version": "1.0",
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "us-east-1:05aab771-99b5-4a9b-8448-de92fe86ba56",
"Region": "us-east-1"
}
}
},
"IdentityManager" : {
"Default" : {
}
}
}
The app runs well importing AWSLambda and the mobileClient, and I'm able to validate credentials with Cognito (I get the "welcome to AWS" message)
Any ideas??
You will have to update your awsconfiguraiton.json file to have information about LambdaInvoker so that it can load the configuration for default service configuration. Your updated file should look like:
{
"Version": "1.0",
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "us-east-1:05aab771-99b5-4a9b-8448-de92fe86ba56",
"Region": "us-east-1"
}
}
},
"IdentityManager" : {
"Default" : {
}
},
"LambdaInvoker" : {
"Default" : {
"Region": "us-east-1"
}
}
}
services.AddDbContext<StoreContext>(options =>
options.UseSqlServer(Configuration["Data:StoreProducts:ConnectionString"]));
In the appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
and in the appsetting.development.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Data": {
"StoreProducts": {
"ConnectionString": "Server=DESKTOP-I3K90LQ\\SQL2016;Database=BrooksStore;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
I am not sure why services threw me an exception saying options.UseSqlServer has null value in it.
Can anyone help?
Thank you!
The value is null because your connection string is in "appsetting.development.json" not "appsetting.json" so you can put your connection string in appsetting.json and it will work just fine.
EDIT
Actually you have a typo in your json file name it's "appsettings.Development.json" not "appsetting.development.json"
notice the 's'
now it should work correctly from your dev json file
How can the Serilog Exceptionless Sink be used with .NET Core 1.1?
The Serilog.Sinks.Exceptionless README isn't clear and doesn't work for .NET Core 1.1 where I have put the configuration in the appsettings.json file.
{
"Serilog": {
"Using": ["Serilog.Sinks.Literate"],
"MinimumLevel": ["Debug"],
"WriteTo": [{
"Name": "LiterateConsole"
}],
"Enrich": ["FromLogContext"],
"Properties": {
"Application": "MyAppServer"
}
}
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger();
}
}
I obviously need to set up the API key somewhere, too.
Can anyone provide a clear description of how this can be configured, please?
In JSON you can add additional sinks to the "WriteTo" list and add arguments like apiKey in the "Args" block:
{
"Serilog": {
"Using": ["Serilog.Sinks.Literate"],
"MinimumLevel": ["Debug"],
"WriteTo": [{
"Name": "LiterateConsole"
}, {
"Name": "Exceptionless",
"Args": { apiKey: "12345" }
}],
"Enrich": ["FromLogContext"],
"Properties": {
"Application": "MyAppServer"
}
}
}
I think it's
Log.Logger = new LoggerConfiguration()
.WriteTo.Exceptionless(
apiKey: "yourApiKey",
additionalOperation: b => b.AddTags("ASP.NET Core Example Logger"))
.CreateLogger();