0

I have seen multiple links on how to do this and my version is techically working.

Example: Example

So, I have added a new property, Unit_Name, to my code.

for (int i = 0; i < 5; i++)
{
    log4net.GlobalContext.Properties["Unit_Name"] = "Bob " + i.ToString();
    log.Fatal("This is fatal");
}

And in the config file for the Console Appender

 <appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger  %property{unit_name} - %message %newline" />
  </layout>
 </appender>

This works, so moving forwards I added this to my AdoNetAdapter section.

      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[UnitName],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger,@unit_name, @message, @exception)" />

and a parameter section

  <parameter>
    <parameterName value="@unit_name" />
    <dbType value="String" />
    <size value="200" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property(Unit_Name)" />
    </layout>
  </parameter>

Now on testing all is still working, but the above parameter should be displaying the string Bob 1, Bob 2, etc. But on looking at the database table, I am getting the following:

{log4net:HostName=GBRCL33199, log4net:Identity=, log4net:UserName=userName, Unit_Name=Bob 0}(Unit_Name)

This must be a config error but I cannot see where or what I have done wrong.

1 Answer 1

0

Thank for those that looked at this.

I knew it must have been something I did. As it happens, changing the conversionPattern string below

<parameter>
    <parameterName value="@unit_name" />
    <dbType value="String" />
    <size value="200" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property(Unit_Name)" />
    </layout>
</parameter>

to

<parameter>
    <parameterName value="@unit_name" />
    <dbType value="String" />
    <size value="200" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{Unit_Name}" />
    </layout>
</parameter>

fixed the problem. Or if you still cannot see it. I changed "%property(Unit_Name)" to "%property{Unit_Name}".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.