Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Framework / Logging / Developer's Guide - Logging - User Identity
In This Topic
    Developer's Guide - Logging - User Identity
    In This Topic

    Loupe captures the current user's name for each log message automatically.  In most cases, this will do exactly what you want - reflecting the OS User that ran the application by default and show a specific authenticated user in the case of a web application. 

    How Loupe Determines the User

    Each time a message is logged, Loupe inspects the Thread.CurrentPrincipal property of the thread logging the message. If this property is null, the identity property is null, or the Identity name is empty then the OS process user name is used.

    The Thread.CurrentPrincipal property is automatically updated by most security systems and correctly copied from thread to thread during asynchronous operations (like using ThreadPool.QueueUserWorkItem or Task.Start).

    Integrating With Your Security System

    If you've written your own authentication system - say for a WinForms Point of Sale application or a background service processing requests on behalf of users - you'll want to take advantage of the built-in .NET Thread.CurrentPrincipal property to set your own user information.

    The best approach is likely to have your own user object implement either IPrincipal or IIdentity (which you would then wrap with a GenericPrincipal object).  Then you can set the user information to the Thread.CurrentPrincipal property and everything will just work.  For the best results, review Developer's Guide - Capturing Application Users to make it easy to populate an Application User.

    Thread.CurrentPrincipal = yourUserObjectImplementingIPrincipal;
    
    Thread.CurrentPrincipal = yourUserObjectImplementingIPrincipal
    

    Alternately, you can use the built-in .NET GenericPrincipal and GenericIdentity classes.  Just create an instance of these and set the user name you want.

    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Your User Name"), null);
    
    Thread.CurrentPrincipal = New GenericPrincipal(New GenericIdentity("Your User Name"), Nothing)
    

    Resetting Identity

    When you want to clear the identity setting you previously made (perhaps because the user logged out or the user-specific task completed) just set the CurrentPrincipal property back to null.  Loupe will then fall back to the OS process user name.

    Enhancing Application User Information

    By default, Loupe captures just the user name of the Current Principal associated with each thread as it writes log entries.  This provides at best a simple user name, typically in the form of <Domain>\<User> or <User Name>.  This is a good start, but there is only so far Loupe can do with this limited amount of information.

    To maximize the benefit you get from Loupe's Application User tracking you'll want to provide additional information for each unique user of your application.  To do this, you need to:

    1. Subscribe to the ResolveApplicationUser Event
    2. Translate each IPrincipal (which includes the user name and other data your security system may be storing) into a full user profile.
    See Also