question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shared property not working

See original GitHub issue

Im trying to write logs and have the UI read them simultaneously, but when I call File.Open(Path.Combine(directory, fileInfo.Name), FileMode.Open, FileAccess.Read)) it throws an exception and the file is already being used by a process.

The log is configured as such: sublogger -> filter -> sinks.map -> sinks.async -> sinks.file So logged items are logged to a uniquely named file filtered by their type and written asynchronously.

Config code:

          public SerilogProvisioner(LoggerConfiguration configuration, LogConfigResolver logConfigResolver, 
         string rootPath)
        {
            Relay = new LogRelay();
            LogConfigResolver = logConfigResolver;

            foreach (var key in logConfigResolver.GetTypes())
            {
                var resolution = logConfigResolver.Resolve(key);

                if (resolution != null && resolution.Output != null)
                {
                    configuration = configuration
                        .WriteTo.Logger(lc => lc
                            .Filter.ByIncludingOnly(Matching.FromSource(key.FullName))
                            .WriteTo.Map(resolution.PropertySelector ?? "LogId", (property, wt) =>
                            {
                                var logOptions = logConfigResolver.Resolve(key);
                                var path = Path.Combine(rootPath, logOptions.Output.Uri, property + "-.txt");

                                var logId = new LogId(key, property);
                                var relaySink = new LogRelaySink(logId, Path.Combine(rootPath, logOptions.Output.Uri));
                                Relay.AddRelay(relaySink);

                                switch (logOptions.Output.OutputType)
                                {
                                    case LogOutputType.File:
                                        wt.Async(a => a.File(
                                            path,
                                            shared: true,
                                            rollingInterval: RollingInterval.Day));
                                        break;
                                }

                                wt.Sink(relaySink);
                            }));
                }
            }
`

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jaksonlincommented, Apr 30, 2019

Hit the same issue when we do read all lines of the log file or using file stream without adding FileShare.ReadWrite:

Logger Configuration:

    logger = new LoggerConfiguration()
            .MinimumLevel.ControlledBy(loggingLevelSwitch)
            .Enrich.WithProperty("Component", component)
            .WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} [{Component}][{Level}] {Message}{NewLine}{Exception}")
            .WriteTo.File(logFile, shared:true, outputTemplate: "{Timestamp:HH:mm:ss} [{Component}][{Level}] {Message}{NewLine}{Exception}")
            .WriteTo.Logger(sLogger)
            .CreateLogger();

Code that hit this bug (file refer to the log file user trying to read):

    string[] result1 = File.ReadAllLines(file);  <--- this won't work
    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))<---this won't work

W/A for this bug:

    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

Add FIleShare.ReadWrite

0reactions
mpowriecommented, Dec 20, 2020

I find that real-time monitoring of log files with is not accurate for long running processes that are using the Serilog logging framework, because Serilog often has a lock on the file even with shared: true

The Serilog config snippet I’m using: Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.File(logFile, shared: true) .CreateLogger();

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shared Ownership Top 10 Issues
Learn the 10 best ways to prevent owning with friends from leading to anger, resentment and frustration.
Read more >
How to Resolve a Joint Property Ownership Dispute
Joint ownership can make the process of purchasing property more affordable. Read our guide to joint ownership & find out how to resolve...
Read more >
Shared ownership: why is it not working for first-time buyers?
Shared ownership has had a bad press in recent years, with negative stories about lack of security, high service charges and negative equity. ......
Read more >
VB Property not being set when setting it from another form
I got it to work by adding the Shared setting on the property. Public Shared Property ColumnWidths As Dictionary(Of Integer, Integer) now it ......
Read more >
Co-Ownership: Legal Issues with Jointly Owned Property
Disputes may sometimes occur in property co-ownership. Find legal ways to settle your case, read this article to learn more.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found