-
Notifications
You must be signed in to change notification settings - Fork 26
Description
I have both a web app and a console app (both .NET Core 3.1) that I am trying to add the MySql sink to. They share identical appsettings.json files, and the standard console sink works on both. However, I can only see logs written through the MySql sink when running the web app. My console app only logs to the console sink, not the MySql sink. I enabled the SelfLog to show the output to console, and it shows "Sending batch of 1 logs" but I see nothing else to indicate an error in the MySql sink. The only difference is in the inherent difference in setting up a logger for a console app vs the web host.
I have tried "manually" configuring the sinks instead of reading from appsettings.json, and I get the exact same output (logs write to console, not to MySql). My console app logic is below. Again, this same configuration works in the web app, so there is either an issue with my console logic, an issue in the Sink, or an error happening inside the sink (like permissions?) that is not getting surfaced through the SelfLog. What am I missing?
namespace Engine
{
class Program
{
private static Dictionary<string,string> ArgDict = new Dictionary<string, string>();
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now.ToLongTimeString());
Console.WriteLine(args.Length);
#if DEBUG
Array.Resize(ref args, 4);
args[0] = "Engine.exe";
args[1] = "-assoc";
args[2] = "0";
args[3] = "-temp";
#endif
for (int i = 0; i <= args.Length - 1; i++)
{
if (args[i].Substring(0, 1) == "-")
{
if (i < args.Length - 1 && args[i + 1].Substring(0, 1) != "-")
{
ArgDict.Add(args[i].ToLower(), args[i + 1].ToLower());
}
else
{
ArgDict.Add(args[i].ToLower(), "");
}
}
}
var services = ConfigureServices();
var serviceProvider = services.BuildServiceProvider();
ServiceActivator.Configure(serviceProvider);
// calls the Run method in App, which is replacing Main
serviceProvider.GetService<App>().Run(ArgDict);
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
private static IServiceCollection ConfigureServices()
{
var services = new ServiceCollection();
var config = LoadConfiguration();
var serilogLogger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration: config)
//.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
//.WriteTo.MySQL("Datasource=localhost;database=mydb;uid=mysqluser;pwd=mysqlpassword;","serilog")
.CreateLogger();
Serilog.Debugging.SelfLog.Enable(Console.Error);
serilogLogger.Information("test console log");
services.AddLogging(configure => configure.AddSerilog(logger:serilogLogger));
Log.Logger = serilogLogger;
services.AddTransient<App>();
return services;
}
public static IConfiguration LoadConfiguration()
{
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Console.WriteLine("Environment: {0}", environment);
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment}.json");
#if DEBUG
builder.AddUserSecrets<Program>();
#endif
return builder.Build();
}
}
}
