2013-06-30 2 views
1

어떻게 NLog를 사용하여 사용자 별 로그 파일을 사용자 이름과 동일한 파일 이름으로 만들 수 있습니까? 레이아웃에서 변수를 사용할 수 있지만 파일 이름 특성은 대상 수준에서 설정된다는 것을 알고 있습니다. 나는 filename="C:\pathtologfiles\${myApp:user}.txt"과 같은 것을 할 수 있고, 호출 클래스에서는 ClassLogger.Debug("user did something", thisUser.Username)을 할 수 있기를 바란다.NLog 사용자 별 로그 파일

답변

0

다음은 VB에서 (불행히도) 끝내는 방법입니다. 물론 _UsernameIsMeaningfulInThisContext_GetUsername을 구현해야합니다. 호출 클래스에서 nlog 설정

<variable name="AppLogDir" value="C:\inetpub\ApplicationLogging\MyApplication" /> 
... 
<targets> 
    ... 
    <target name="UserSpecificLogfile" 
      xsi:type="File" 
      fileName="${AppLogDir}\Users\AppUser_${event-context:item=AppUsername}.txt" 
      createDirs="true" /> 
</targets> 
<rules> 
    ... 
    <logger name="*" minLevel="Trace" maxLevel="Fatal" writeTo="UserSpecificLogfile" /> 
</rules> 

에서

Private Property _ClassLogger As Logger = LogManager.GetCurrentClassLogger() 
... 
Private Sub _LogMaybeUser(ByVal nLogLevel As NLog.LogLevel, ByVal nMsg As String) 
{ 
    Dim logfileUser As String = "Unknown" 
    If _UsernameIsMeaningfulInThisContext() { logfileUser = _GetUsername() } 
    Dim logInfo As New LogEventInfo(nLogLevel, "", nMsg) 
    logInfo.Properties("AppUsername") = logfileUser 
    ClassLogger.Log(logInfo) 
}