Skip to content

PathRoller doesn't log to SelfLog on exception #192

@pvandervelde

Description

@pvandervelde

When the PathRoller encounters an exception it doesn't log to the Serilog self-log. Using the following code

var selfLogPath = "c://logs//serilog.log";
string logsDirectory = Path.GetDirectoryName(selfLogPath);
if (!Directory.Exists(logsDirectory))
{
    Directory.CreateDirectory(logsDirectory);
}

Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(File.CreateText(selfLogPath)));

var serilogger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    .CreateLogger();

serilogger.Information("message");

with the following app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
    <add key="serilog:minimum-level" value="Verbose" />

    <add key="serilog:using:Console" value="Serilog.Sinks.Console"/>
    <add key="serilog:write-to:Console"/>

    <add key="serilog:using:File" value="Serilog.Sinks.File" />
    <add key="serilog:write-to:File.fileSizeLimitBytes" value="104857600" />
    <add key="serilog:write-to:File.formatter" value="Serilog.Formatting.Json.JsonFormatter" />
    <add key="serilog:write-to:File.path" value="xxxc:/logs/test-log.log" />
    <add key="serilog:write-to:File.rollOnFileSizeLimit" value="true" />
</appSettings>
</configuration>

Running this code throws the following exception

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotSupportedException: The given path's format is not supported.
    at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
    at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
    at Serilog.Sinks.File.PathRoller..ctor(String path, RollingInterval interval)
    at Serilog.Sinks.File.RollingFileSink..ctor(String path, ITextFormatter textFormatter, Nullable`1 fileSizeLimitBytes, Nullable`1 retainedFileCountLimit, Encoding encoding, Boolean buffered, Boolean shared, RollingInterval rollingInterval, Boolean rollOnFileSizeLimit, FileLifecycleHooks hooks)
    at Serilog.FileLoggerConfigurationExtensions.ConfigureFile(Func`4 addSink, ITextFormatter formatter, String path, LogEventLevel restrictedToMinimumLevel, Nullable`1 fileSizeLimitBytes, LoggingLevelSwitch levelSwitch, Boolean buffered, Boolean propagateExceptions, Boolean shared, Nullable`1 flushToDiskInterval, Encoding encoding, RollingInterval rollingInterval, Boolean rollOnFileSizeLimit, Nullable`1 retainedFileCountLimit, FileLifecycleHooks hooks)
    at Serilog.FileLoggerConfigurationExtensions.File(LoggerSinkConfiguration sinkConfiguration, ITextFormatter formatter, String path, LogEventLevel restrictedToMinimumLevel, Nullable`1 fileSizeLimitBytes, LoggingLevelSwitch levelSwitch, Boolean buffered, Boolean shared, Nullable`1 flushToDiskInterval, RollingInterval rollingInterval, Boolean rollOnFileSizeLimit, Nullable`1 retainedFileCountLimit, Encoding encoding, FileLifecycleHooks hooks)
    --- End of inner exception stack trace ---
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at Serilog.Settings.KeyValuePairs.KeyValuePairSettings.ApplyDirectives(List`1 directives, IList`1 configurationMethods, Object loggerConfigMethod, IReadOnlyDictionary`2 declaredSwitches)
    at Serilog.Settings.KeyValuePairs.KeyValuePairSettings.Configure(LoggerConfiguration loggerConfiguration)
    at Serilog.Configuration.LoggerSettingsConfiguration.Settings(ILoggerSettings settings)
    at Serilog.Configuration.LoggerSettingsConfiguration.KeyValuePairs(IEnumerable`1 settings)
    at Serilog.Configuration.LoggerSettingsConfiguration.Settings(ILoggerSettings settings)
    at MyApp.Tests.ConsoleApp.Program.LogWithSerilogDirect(String selfLogPath)
    at MyApp.Tests.ConsoleApp.Program.Main()

This exception terminates the application because it is not caught anywhere. Given that Serilog internally handles exceptions and that we have turned on the self-log I would have expected that the PathRoller would have caught the exception and then written it to the selflog.

We can of course catch the exception but because the exception occurs during the configuration of our logging system it means that it is more difficult to store the information in a way that we can obtain it from the server.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions