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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With