Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ShaharPrishMSFT opened this issue Nov 29, 2020 · 6 comments
Closed

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

ShaharPrishMSFT opened this issue Nov 29, 2020 · 6 comments
Labels

Comments

@ShaharPrishMSFT
Copy link

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
Copy link
Owner

adams85 commented Nov 30, 2020

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
Copy link
Author

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.b__1_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.OptionsMonitor1.<>c__DisplayClass11_0.<Get>b__0() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode)

@ShaharPrishMSFT
Copy link
Author

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
Copy link
Owner

adams85 commented Dec 1, 2020

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
Copy link
Owner

adams85 commented Dec 1, 2020

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
Copy link
Author

Thanks. That solved my isseus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants