adams85 / filelogger

A lightweight yet feature-rich file logger implementation for the Microsoft.Extensions.Logging framework.
MIT License
147 stars 22 forks source link

No logs are created - what am I missing? #12

Closed ShaharPrishMSFT closed 3 years ago

ShaharPrishMSFT commented 3 years ago

I have the following code - nothing in appsettings or anywhere...

                configLogging.AddProvider(new SimpleConsoleLogger())
                    .AddFile(c =>
                    {
                        string dir = Path.Combine(Path.GetTempPath(), "Paros.Logs");
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        c.RootPath = dir;
                        c.MaxFileSize = 10_000_000;
                        c.FileAccessMode = Karambolo.Extensions.Logging.File.LogFileAccessMode.KeepOpenAndAutoFlush;
                    });

And no logs are being created.

What else am I missing?

adams85 commented 3 years ago

Is this your full configuration? No appsettings.json?

If so, please refer to the docs on configuration:

Description Default value Notes
Files An array of LogFileOptions which define the settings of the individual log files. There is an important change compared to the older versions: you must explicitly define at least one log file, otherwise the provider won't log anything.

Otherwise, please verify that the process has the sufficient file system permissions on the RootPath directory. (As you're using a temp directory, this shouldn't be the problem.)

On a side note, the logger creates the log directory if it doesn't exist, so you can safely remove the following lines:

if (!Directory.Exists(dir))
{
    Directory.CreateDirectory(dir);
}
ShaharPrishMSFT commented 3 years ago

If I don't create the folder, I get an exception on setting the value to Root:

System.IO.DirectoryNotFoundException HResult=0x80070003 Message=C:\Users\shaharp\AppData\Local\Temp\Paros.Logs\ Source=Microsoft.Extensions.FileProviders.Physical StackTrace: at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters) at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root) at Karambolo.Extensions.Logging.File.PhysicalFileAppender..ctor(String root) at Karambolo.Extensions.Logging.File.FileLoggerOptions.set_RootPath(String value) at ComputeService.Program.<>c.b1_4(FileLoggerOptions c) in C:\src\wicd\TVM.ComputeService\ComputeService\Program.cs:line 144 at Microsoft.Extensions.Options.ConfigureNamedOptions1.Configure(String name, TOptions options) at Microsoft.Extensions.Options.OptionsFactory1.Create(String name) at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass11_0.b0() at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)

ShaharPrishMSFT commented 3 years ago

What's the minimum set of properties I need to set on the the single File object I create in the Files array? Just creating an array with a single instantiation of the LogFileOptions class, does not work either.

adams85 commented 3 years ago

Ok, let's first make clear which version of the lib you are using. My answer applies to v3.0 or later. In older versions the log directory wasn't created automatically unless you set EnsureBasePath to true.

adams85 commented 3 years ago

Sorry for my oversight. FileAccessMode was introduced in v3.0, so you obviously aren't using an older version.

Meanwhile I realized that my answer wasn't completely right, either. RootPath must exist as this is the root directory of the underlying file provider. So the correct setup in your case is something like this:

.AddFile(c =>
{
    c.RootPath = Path.GetTempPath();
    c.BasePath = "Paros.Logs";
    c.MaxFileSize = 10_000_000;
    c.FileAccessMode = Karambolo.Extensions.Logging.File.LogFileAccessMode.KeepOpenAndAutoFlush;
    c.Files = new[]
    {
        new Karambolo.Extensions.Logging.File.LogFileOptions { Path = "default.log" }
    };
});

What's the minimum set of properties I need to set on the the single File object I create in the Files array? Just creating an array with a single instantiation of the LogFileOptions class, does not work either.

As you can see above, you need to specify at least the Path property of the log file (which is relative to BasePath).

ShaharPrishMSFT commented 3 years ago

Thanks. That solved my isseus.