8

I have a Logger.cs class:

public Logger(string logtype=null)
        {
            _logtype = logtype;
            LogEventLevel level = LogEventLevel.Warning;
            if (File.Exists(configPath))
            {
                XDocument xdoc = XDocument.Load(configPath);
                string val = xdoc.Descendants("logEnabled").First().Value;
                // if(clientConfig.LogEnabled == true)
                if (val == "true")
                {
                    level = LogEventLevel.Debug;

                }
                else if (val == "false")
                {
                    level = LogEventLevel.Warning;
                }
            }
            else
            {
                level = LogEventLevel.Warning;
            }
           _logger = new LoggerConfiguration()
                        .MinimumLevel.Debug()
                         .WriteTo.File(_filepath, level)
                         .CreateLogger();

        }

this logger class is used in multiple projects in a solution. Like shown below:

    Class A {
     Logger _logger = new Logger("A");
    }

Class A1 {
     Logger _logger = new Logger("A");
    }

Class B {
     Logger _logger = new Logger("B");
    }

Class B1 {
     Logger _logger = new Logger("A");
    }

With my above code. Now i can see only the first logging is written to the log file. Class A and Class A1 are using the same log file "A", when my program executes i can see only Class A logging is present in the log but not class A1 logging messages. Same for Class B and Class B1, only class B messages are visible.

How can i use the same log file across multiple project in a solution?

1 Answer 1

14

You will need to enable multi-process shared log files, set shared to true

_logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.File(_filepath, level, shared: true)
                .CreateLogger();

For more check Shared log files

Sign up to request clarification or add additional context in comments.

2 Comments

How can this property set via appsetting.json?
@user1066231 Try "WriteTo": [{ "Name": "File", "Args": { "path": "log.txt", "shared": "true" } }]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.