log4net using ThreadContext.Properties in wcf PerSession service

前端 未结 2 1671
情深已故
情深已故 2021-01-19 02:03

I would like to use the following in my wcf service to log the user in the log message:

log4net.ThreadContext.Properties[\"user\"] = this.currentUser.Login         


        
2条回答
  •  执念已碎
    2021-01-19 02:43

    Log4net supports "calculated context values". By using this you could write a class like this:

    public class UserNameContext
    {
        public override string ToString()
        {
            string userName = ...; // get the name of the current user
    
            return userName;
        }
    }
    

    If you add this to the global context you can access the property in your appenders (like you are used to). The 'ToString' method will be executed every time and thus you get the correct user name.

    More on context values can be found in this great tutorial: http://www.beefycode.com/post/Log4Net-Tutorial-pt-6-Log-Event-Context.aspx

提交回复
热议问题