Justin
Justin

Reputation: 154

Can't get user information into Serilog plain text log

I'm trying to implement some simple logging into an application. I am using Serilog, but due to circumstances out of my control, I can't use Seq as the sink. For now, I've been told to simply write the logs to a local file.

I am able to successfully log basic information, but am having trouble with getting the user's information into the logs. Serilog.Enrichers.Environments package gets the information just fine, but the methods only enrich the logs, which aren't seen in a plain text log. When I use Seq as the sink (which I can't use for my final implementation) I can see the enrichments being logged just fine.

Currently I am trying to get the user info from the HttpContext but it is returning null. Here is an example of code that I've tried:

public static class HTMLHelperExtensions
{
    public static string CurrentUser
    {
        get
        {
            HttpContext httpContext = HttpContext.Current;
            if (httpContext == null || httpContext.User == null)
                return null;

            return httpContext.User.Identity.Name;
        }
    }
}

Here is the logging configuration in my Global.asax:

Log.Logger = new LoggerConfiguration()
            .Enrich.WithEnvironmentUserName()
            .WriteTo.File(
                "fileLocation.txt",
                restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information,
                rollingInterval: RollingInterval.Infinite,
                fileSizeLimitBytes: 10485760, //10MiB
                retainedFileCountLimit: 50 )                
            .WriteTo.Seq("http://localhost:5341")
            .CreateLogger();

And where I'm calling the code from:

public class HomeController : Controller
{
    public ActionResult Index()
    {

        Log.Information("Application initial load. Included in Audit Log: {auditLog}. User: {User}", true, HTMLHelperExtensions.CurrentUser);
        //other code

        return View();
    }
}

The .Enrich.WithEnvironmentUserName() method works just fine, but I don't know how to get that information into the actual body of the log instead of an enrichment.

Upvotes: 2

Views: 698

Answers (2)

Ruben Bartelink
Ruben Bartelink

Reputation: 61903

Not sure what you're looking for, but you can build a custom enricher with:

class CurrentUserEnricher : Serilog.Core.ILogEventEnricher
{
    public void Enrich(Serilog.Events.LogEvent logEvent, Serilog.Core.ILogEventPropertyFactory lepf) =>
        logEvent.AddPropertyIfAbsent(lepf.CreateProperty("CurrentUser", HTMLHelperExtensions.CurrentUser));
}

And include in pipeline with a:

.Enrich.With<CurrentUserEnricher>()

Then as long as {Properties} or {CurrentUser} is in your render format, you'll get the value included ?

Upvotes: 1

Justin
Justin

Reputation: 154

I figured it out for now. If I use httpContext.Request.LogonUserIdentity.Name instead of httpContext.User.Identity.Name it gives me the information I want.

If there is a way to use the Serilog configuration to append this info to the log body instead of how I'm doing it, I will accept that answer.

Upvotes: 1

Related Questions