serilogserilog-sinks-file

Serilog file sink not writing logging when executed from a sql job


The dilemma:

I've added logging to an existing console application for work that is executed every 5 minutes via a sql job. The logging has worked in development, testing, and production when I execute the program. The logging doesn't happen when the application is executed from a sql job. The application does appear to complete it's task successfully each time it is run by the sql job though. Write permissions for the account running the sql job to the log folder have been verified.

The setup:

Application is a console app on .net 4.8 framework.

Serilog nuget packages used:

<package id="Serilog" version="2.12.0" targetFramework="net48" />
<package id="Serilog.Enrichers.Environment" version="2.2.0" targetFramework="net48" />
<package id="Serilog.Sinks.Console" version="4.1.0" targetFramework="net48" />
<package id="Serilog.Sinks.File" version="5.0.0" targetFramework="net48" />

Serilog configuration:

public static void Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .Enrich.WithMachineName()
        .Enrich.WithEnvironmentUserName()
        .MinimumLevel.Debug()
        .WriteTo.Console()
        .WriteTo.File("logs/applicationName_.txt",
            rollingInterval: RollingInterval.Day,
            outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {MachineName} {EnvironmentUserName} | {Message}{NewLine}{Exception}")
        .CreateLogger();
    ...

Solution

  • You should change the log file path to be absolute.

    .WriteTo.File("C:\logs\applicationName.txt",