1

Below is my current pattern layout set:

    %d{%m/%d/%Y %H:%M:%S,%l} [%t]  %-5p %c %x %m%n

So it outputs as below to the log file;

    06/13/2017 10:57:44,632 [3684]  INFO  MyLogCategory File: myclass.cpp Line: 59  My Logged Message

I would like to add current Windows User name to the appender so as I will get following output.. but no luck so far. I don't wanna add Windows User name for every message instead log4cpp should fetch current username and attach with every message it logs (I don't know log4cpp has this support yet. I found a pattern key "H" or "h" but it isn't working and throwing unknown conversion specifier 'H'.

    06/13/2017 10:57:44,632 [3684][**WINDOWS USER NAME**]  INFO  MyLogCategory File: myclass.cpp Line: 59  My Logged Message
wonk
  • 23
  • 7

1 Answers1

1

I was able to append username to the log using NDC (Nested Diagnostic Context).

#include "log4cpp\NDC.hh"

Then I pushed the context for every thread (Yeah NDC is thread specific, it won't be visible to all threads unless you set it). In my case, I pushed Windows Current User name. Then user name gets automatically added into all logs which was added under the thread which has NDC set.

CString sUserContext = "[" + GetCurrentUserName() + "]";
std::string userContextStr = CT2CA(sUserContext);
log4cpp::NDC::push(userContextStr);
wonk
  • 23
  • 7