1

I've been unable to figure this one out and am wondering if anyone has any ideas. This is an ASP.NET Core Web API running as a Windows service.

It will log to a file at the absolute path I specify for startup and until one or a few web requests come in, and then it just stops.

It's also logging to a file in system/windows32.

I've not seen any output ever into a serilog self-log file.

What I've tried:

  • Adding serilog self-logging
  • Calling builder.Services.AddSerilog per the example from the docs
  • Not passing Log.Logger in to the UseSerilog call since it should just use it anyways
  • Searching for similar issues, finding reports of serilog logging to windows/system32 being due to not using an absolute path or setting the working directory to something else (does not apply since I am using an absolute path)
  • Numerous other things I can't remember

Code - program.cs:

using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.FileProviders;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using Serilog.Extensions.Logging;
using Log = Serilog.Log;

public static class Program
{
    public static void Main(string[] args)
    {
        try
        {
            // set current directory to workign directory

            var builder = WebApplication.CreateBuilder();
            
            builder.Logging.ClearProviders();
            ServiceUtils.SetupLog();
            builder.Host.UseSerilog(Log.Logger);

            var process_name = Assembly.GetEntryAssembly()?.GetName().Name ?? "";
            Log.Information(
                $"\n\n***** Starting {process_name} on {Environment.MachineName} with command {Environment.CommandLine}\n"
            );

            builder.Host.UseWindowsService();
            // ...

            app.UseSerilogRequestLogging();
            // ...
        }
        catch()
        {
            // ..
        }
    }
}

From the SetupLog method:

using System.Diagnostics;
using System.Reflection;
using System.Runtime.Versioning;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using Serilog.Events;

public static void SetupLog()
{
    var entry = Assembly.GetEntryAssembly().GetName();
    //var base_dir = AppContext.BaseDirectory;
    var base_dir = "C:\\some-dir";

    var log_dir = Path.Combine(base_dir, $"{entry.Name}_log");
    var log_name = $"{Process.GetCurrentProcess().ProcessName}_{Environment.MachineName}.log";
    var log_path = Path.Combine(log_dir, log_name);

    Serilog.Debugging.SelfLog.Enable(msg =>
            {
                File.AppendAllText(@$"{base_dir}\serilog-selflog.txt", msg + Environment.NewLine);
            });

    Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
                .MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning)
                .MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning)
                .Enrich.FromLogContext()
                .Enrich.WithProcessId()
                .Enrich.WithMachineName()
                .Filter.ByExcluding(
                    (e) =>
                    {
                        var path = e.Properties.ContainsKey("RequestPath")
                            ? e.Properties["RequestPath"].ToString().Replace("\"", "")
                            : "";
                        return HealthCheckIgnorePaths.Contains(path);
                    }
                )
                .WriteTo.Debug(restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug)
                .WriteTo.Console(restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
                .WriteTo.File(log_path, rollingInterval: RollingInterval.Day)
                .WriteTo.NewRelicLogs(
                    endpointUrl: "https://log-api.newrelic.com/log/v1",
                    applicationName: $"{entry.Name}",
                    licenseKey: someKey
                )
                .CreateLogger();

    Log.Debug($"Beginning application log.\nLog file path: [{log_path}]\n");
}

0

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.