Saurabh Dave
Saurabh Dave

Reputation: 1

NLog with LogEventInfo

I am using NLog 4.4.9 to log the info/error/exception. I am using following config settings and code to log exception. It logs other information but does not log exception. What am I doing anything wrong?

Target setting for NLog

<target name="RollingFile" 
    layout="${log4jxmlevent:includeAllProperties=true}"
    type="File" 
    fileName="${LogDir}/webapi.log" 
    encoding="utf-8" 
    maxArchiveFiles="10" 
    archiveNumbering="Sequence" 
    archiveAboveSize="1048576" 
    archiveFileName="${LogDir}/{#######}.a">

Code file

Logger = LogManager.GetLogger("ABC.Error");
LogEventInfo logEventInfo = new LogEventInfo();
logEventInfo.Level = LogLevel.Info;
logEventInfo.Message = "This is test message";
logEventInfo.Properties["UserId"] = "DAVESAURABH";
logEventInfo.Exception = e;
Logger.Log(LogLevel.Info, logEventInfo);

Upvotes: 0

Views: 1106

Answers (1)

Stephen Patten
Stephen Patten

Reputation: 6363

Sometimes the default layout does NOT include the error e.g.

${longdate}|${level:uppercase=true}|${logger}|${message}

You'll have to add it manually e.g.

${longdate}|${level:uppercase=true}|${logger}|${message}|${error}

Hope this is what you're looking for.

Upvotes: 1

Related Questions