I have created a .net framework windows service app, that should use serilog and MS SQL Server to log messages and any potential errors. My issue is that nothing is being logged to MS SQL Server, so I am not sure what the issue is.
As an example (I removed my sensitive code) - I have in the publice Service1()
method my initialization for SeriLog, then I want a process to run every 5 minutes, so in the OnStart()
I have the timer set-up and I use this code
public Service1()
{
InitializeComponent();
var log = new LoggerConfiguration()
.WriteTo.MSSqlServer(
connectionString: "data source=ZZZZZZZZ;Initial Catalog=ZZ;Persist Security Info=False;User Id=ZZ; Password=ZZZ;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;",
sinkOptions: new SinkOptions
{
TableName = "SeriLog",
SchemaName = "dbo",
AutoCreateSqlTable = false
})
.CreateLogger();
}
protected override void OnStart(string[] args)
{
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 300000;
timer.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
GetData().GetAwaiter().GetResult();
}
private static async Task GetData()
{
Log.Information("Started Grabbing Data");
}
Now does anyone see anything that would keep the logging from occuring?
EDIT
I have used this exact code in a console app and it is writing to SQL Server, but when I publish my Windows Service App and have it running as a service, no logs are ever recorded.
You're initializing var log
but logging through the static Log
class. To enable the static Log
class, you need to initialize Log.Logger
.