Created
June 26, 2020 15:09
-
-
Save AndrewAllison/8c6d951750e7c72b21b62203a3e559b2 to your computer and use it in GitHub Desktop.
SeriLogger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Serilog": { | |
"MinimumLevel": { | |
"Default": "Debug", | |
"Override": { | |
"System": "Information", | |
"Microsoft": "Warning", | |
"Hangfire": "Debug" | |
} | |
}, | |
"WriteTo": [ | |
{ | |
"Name": "Seq", | |
"Args": { "serverUrl": "http://localhost:5341" } | |
} | |
], | |
"Enrich": [ "FromLogContext", "WithCorrelationId", "WithCorrelationIdHeader", "WithThreadId", "WithMachineName", "WithEnvironmentUserName" ] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Logging; | |
using Serilog; | |
using Serilog.Core; | |
using System; | |
using System.Collections.Generic; | |
using Xunit; | |
namespace HangFire.Jobs.E2ETests | |
{ | |
public class LoggerTesting | |
{ | |
//TODO: SHOULD be integration or E2E Tests | |
private ILogger<LoggerTesting> logger; | |
private readonly IConfiguration configuration; | |
public LoggerTesting() | |
{ | |
configuration = GetConfiguration(); | |
} | |
public static IConfiguration GetConfiguration(string environment = "Development") | |
{ | |
return new ConfigurationBuilder() | |
.AddJsonFile("appsettings.json") | |
.AddJsonFile($"appsettings.{environment}.json") | |
.AddEnvironmentVariables() | |
.Build(); | |
} | |
public static Logger CreateSerilogger(IConfiguration configuration) | |
{ | |
var logger = new LoggerConfiguration() | |
.ReadFrom.Configuration(configuration) | |
.Enrich.WithProperty("Application", "Testing") | |
.Enrich.WithProperty("Environment", "Test") | |
.CreateLogger(); | |
Log.Logger = logger; | |
return logger; | |
} | |
private ILogger<T> GetLogger<T>(Logger loggs) | |
{ | |
var loggerFactory = (ILoggerFactory)new LoggerFactory(); | |
loggerFactory.AddSerilog(loggs); | |
return loggerFactory.CreateLogger<T>(); | |
} | |
[Fact] | |
public void Should_Upload_Products() | |
{ | |
using var loggs = CreateSerilogger(configuration); | |
var requestTraceId = $"Test-{DateTime.Now.ToString("yyyyMMddhhmmss")}"; | |
logger = GetLogger<LoggerTesting>(loggs); | |
var options = new Dictionary<string, object> { { "requestTraceId", requestTraceId } }; | |
using (logger.BeginScope(options)) | |
{ | |
logger.LogInformation("Should_Upload_Products starting"); | |
logger.LogDebug("Should_Upload_Products starting"); | |
logger.LogWarning("Should_Upload_Products starting"); | |
logger.LogError("Should_Upload_Products starting"); | |
logger.LogInformation("Should_Upload_Products complete"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment