serilog / serilog-sinks-file

Write Serilog events to files in text and JSON formats, optionally rolling on time or size
Apache License 2.0
335 stars 119 forks source link

Separate logs for every project in solution - created only one of them #230

Closed troncomputers closed 2 years ago

troncomputers commented 3 years ago

Hi. I have three projects Optimo (WinForms), OptimoService (Windows Service) and OptimoLib (Class Library). All in .NET Framework 4.8. In MainForm.cs I have logger for user interactions

private Logger LOG = new LoggerConfiguration()
    .WriteTo.File("mainform.log", rollingInterval: RollingInterval.Day)
    .CreateLogger();

With this form user is able to start a Windows Service (OptimoService) which has its own logger created

private Logger LOG = new LoggerConfiguration()
    .WriteTo.File("service.log", rollingInterval: RollingInterval.Day)
    .CreateLogger();

Inside OptimoLib I have another logger that is handling executed jobs using FluentScheduler.

USAGE

LOG.Information("ORDER_JOB created");

Non of those additional are created (Service and Lib). Only WinForms creates log file with

2021-07-02 13:15:24.106 +02:00 [INF] Launching service....

OptimoService OnStart()

            try
            {
                LOG.Information("Starting service");
                _sql = new SqlService();
                _appConfig = _sql.LoadConfiguration();
                _optima = new OptimaService(_sql, _appConfig);

                TimerService.CreateSchedules(_appConfig, _optima, _sql);
                LOG.Information("Service started. Timers setup");
            }
            catch(Exception ex)
            {
                LOG.Error(ex, "ServiceStart");
            }

TIMER - ORDER JOB Execute()

        public void Execute()
        {
            LOG.Information("[ORDER_JOB] Starting");
            EmpikService empik = new EmpikService(_appConfig, _sql);

            try
            {
                LOG.Information("[ORDER_JOB] GetAndAcceptNewOrders()");

                empik.GetAndAcceptNewOrders();
            }
            catch (Exception ex)
            {
                LOG.Error(ex, "[ORDER_JOB] - GetAndAcceptNewOrders()");
            }

            try
            {
                var orders = empik.GetOrdersForShipping();

                for (int i = 0; i < orders.Orders.Count; i++)
                {
                    var order = orders.Orders[i];
                    try
                    {
                        LOG.Information("[ORDER_JOB] - InsertOrder " + order.OrderId);
                        _optima.InsertOrder(order);
                    }
                    catch (Exception ex)
                    {
                        LOG.Error(ex, "[ORDER_JOB] - InsertOrder()");
                    }
                }
            }
            catch(Exception ex)
            {
                LOG.Error(ex, "[ORDER_JOB] - GetOrdersForShipping()");
            }
        }

What am I doing wrong? Is that a bug?

nblumhardt commented 3 years ago

Hi! Try fully-qualifying the log file paths like "service.log" - they'll otherwise end up wherever the process's current working directory is, which won't always be where an obvious location. HTH!

troncomputers commented 3 years ago

All three apps are in the same place. Logs from WinForms project works correctly. Service .exe and Library .dll files are in the same location but no logs if I specify just a file name instead of absolute path. Does Serilog automatically handle the path if you don't specify it correctly?

nblumhardt commented 2 years ago

Hi! We're triaging issues in this tracker and the current one appears to be stale. If your issue relates to a usage question, asking on Stack Overflow and posting a link here will get the most eyes on it (only a small number of maintainers actively track this repository). If your issue relates to a possible bug that still needs attention, please feel free to comment/request reopening. Thanks!