Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read Object in nlog Layouts #2216

Closed
rams300 opened this issue Jul 18, 2017 · 4 comments
Closed

How to read Object in nlog Layouts #2216

rams300 opened this issue Jul 18, 2017 · 4 comments
Labels
Milestone

Comments

@rams300
Copy link

rams300 commented Jul 18, 2017

I wanted to maintain some common class for logging related information like as below.

 public class LogObject
    {
        public string AppName { get; set; }
        public string StackTrace { get; set; }
        public string Message { get; set; }
        public string JobNumber { get; set; }
        public string TransactionType { get; set; }
    }

Adding entire log object in API, like as below.

 logMgr.Info<LogObject>(logObject);

now i need to print those information into log like as below.

 <layout xsi:type="SimpleLayout"
               text="AppName-- ${logObject.AppName}
                     Message-- ${logObject.Message}"
              >
      </layout>

But above layout render is not working, i am unable to read the object like logObject.Message.
i know other way is adding individual properties into variable and events, i am more looking addding entire object into log and reading in layout.

@snakefoot
Copy link
Contributor

snakefoot commented Jul 19, 2017

Maybe you can to do something like this:

nLogger.Info("{0}", logObject);

Then register a custom layoutrenderer for each LogObject-property like this:

NLog.LayoutRenderers.LayoutRenderer.Register("logObject-AppName", (logEvent) =>
{
    if (logEvent.Parameters?.Count == 1)
    {
        LogObject logObject = logEvent.Parameters[0] as LogObject;
        if (logObject != null)
              return logObject.AppName;
    }
    return string.Empty;
}

Then your layout can look like this:

 <layout xsi:type="SimpleLayout"
               text="AppName-- ${logObject-AppName}
                     Message-- ${logObject-Message}"
              >
 </layout>

Then you could also consider implementing LogObject.ToString(), which NLog would then use for the standard layoutrenderer ${message}.

@rams300
Copy link
Author

rams300 commented Jul 19, 2017

Thanks Snakefoot for quick response and providing the solutions.
I am new to Nlog, i am looking for is there any possibility of accessing object members directly in Layout without writing custom code.

@304NotModified
Copy link
Member

I assume your question has been answered, if not, let us know!

@snakefoot
Copy link
Contributor

NLog 4.6.3 introduces objectpath= as option, so you can specify ${event-properties:item=logObject:objectpath=AppName}. See also #3329

@snakefoot snakefoot added this to the 4.6.3 milestone Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants