2

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?

wageoghe
  • 27,390
  • 13
  • 88
  • 116
prime23
  • 3,362
  • 2
  • 36
  • 52

1 Answers1

3

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.

Community
  • 1
  • 1
wageoghe
  • 27,390
  • 13
  • 88
  • 116
  • looks like same as what I did: read from config file(CommonMethods.ReadConfig("product");) – prime23 Mar 01 '12 at 06:04
  • Do you want to do something different? The way I read the question I thought you might want to be able to configure the LayoutRenderer so that you could read any value from the config file. The idea for a LayoutRenderer that does that by specifying the value name as a parameter to the LayoutRenderer in the NLog.config file does that. Is there something else you are looking for? There might be a better way to do what you want. – wageoghe Mar 01 '12 at 14:37
  • sorry for replying late. what I wanted to do is set value out of Append method, but not give parameter to layoutRender. But since my custom layoutRender (application name) is a const, I just hard coded to sql insert in NLog.config. Thank you again. – prime23 Mar 05 '12 at 06:25