prime23
prime23

Reputation: 3393

Pass value to NLog custom LayoutRenderer

Ok,now I knew how to write a custom layoutRender

  1. write a class MylayoutRender inherits LayoutRenderer, in class,
  2. override Append method
  3. register custom layoutRenderer on startup

    ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("application", typeof(MyLayoutRenderer));

curently, I read the value from config

protected override void Append(StringBuilder builder, LogEventInfo logEvent)
    {
        var product = CommonMethods.ReadConfig("product");
        builder.Append(product); 
    }

how to set the value dynamically?

Upvotes: 2

Views: 3342

Answers (1)

wageoghe
wageoghe

Reputation: 27608

This answer contains an example of a LayoutRenderer that allows you to configure a parameter that says which config value to read.

NLog config file to get configuration setting values from a web.config

From what @DaveHogan posted, if you wrote your own LayoutRenderer and called it MyLayoutRenderer and wanted to log the "product" value, you would configure it something like this:

${MyLayoutRenderer:product}

The key is the [DefaultParameter] attribute decorating the property of the LayoutRenderer that indicates which property to read from the configuration.

This question (from me) shows an example of creating an NLog LayoutRenderer that takes a parameter and then uses that parameter as a key for a lookup. (The subject of the question is log4net, but I was posting an example of something I could do in NLog and wanted an answer showing how to something similar in log4net). Note that the example is for NLog 1.1. It will be slightly different in NLog 2.0.

You might also find this link to the NLog code repository useful.

Upvotes: 3

Related Questions