Search code examples
asp.net-coreserilog

How to customise log file in serilog


Here is the output of my serilog.

 2022-05-24T16:31:13.4340853+05:30  [INF] Application started. Press Ctrl+C to shut down. 
 (dcaefe54)
 2022-05-24T16:31:13.5439262+05:30  [INF] Hosting environment: "Development" (c3307c92)
 2022-05-24T16:31:13.5468423+05:30  [INF] Content root path: 
 "E:\Projects\Eigen\Eigen.UE.WebUI.Razor" (b5d60022)
 2022-05-24T16:31:19.0392934+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 1 
record (d1092e14)
2022-05-24T16:31:19.0761232+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 2 
 record (9758a16f)
2022-05-24T16:31:19.0786689+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 3 
record (ddc237d7)
 2022-05-24T16:31:19.0816914+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 4 
 record (ca210324)
 2022-05-24T16:31:19.0851599+05:30 8000000e-0000-fd00-b63f-84710c7967bb [INF] inserting 5 
 record (d2fab210)

I dont understand where this sort of guid comes in 8000000e-0000-fd00-b63f-84710c7967bb, How do I remove this guid?

This is my startup.cs file

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        loggerFactory.AddFile("Logs/EigenLog-{Date}.txt");

        //app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();

            endpoints.MapControllers();
        });
    }

Solution

  • The GUID is a request id:

    https://github.com/serilog/serilog-extensions-logging-file/#file-format

    To customize it, specify an output template:

    loggerFactory.AddFile("Logs/EigenLog-{Date}.txt", outputTemplate:
        "{Timestamp:o} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}"
    );