Log4Net not work with my customs properties

AuTi picture AuTi · May 21, 2012 · Viewed 8.3k times · Source

in this time I try to log into a DB whit L4N, this is the struct of the table:

[TcpClientTracer]
[ID] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar](20) NOT NULL,
[EventType] [varchar](20) NOT NULL,
[ClientId] [int] NOT NULL,
[ModemId] [varchar](128) NOT NULL,
[EventText] [varchar](2000) NOT NULL

to fill this table I use this appender:

<appender name="TcpAdoNetAppender" type="log4net.Appender.AdoNetAppender,log4net">
  <bufferSize value="50" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="Data Source=[IPADDRES]; Initial Catalog=XYXY; User Id=username;Password=password" />
  <commandText value="INSERT INTO TcpClientTracer ([Date],[Thread],[EventType],[ClientId],[ModemId],[EventText]) VALUES (@log_date, @thread, @eventtype, @clientid, @modemid, @eventtext)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="20" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%thread" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@eventtype" />
    <dbType value="String" />
    <size value="20" />
    <layout type="log4net.Layout.RawPropertyLayout">
      <key value="eventtype" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@clientid"/>
    <dbType value="Int32"/>
    <layout type="log4net.Layout.RawPropertyLayout">
      <key value="clientid" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@modemid" />
    <dbType value="String" />
    <size value="128" />
    <layout type="log4net.Layout.RawPropertyLayout">
      <key value="modemid" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@eventtext" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.RawPropertyLayout">
      <key value="eventtext" />
    </layout>
  </parameter>
</appender>

the logger is this:

<logger name="TcpClientTracer" additivity="false">
  <level value="ALL"/>
  <appender-ref ref="TcpAdoNetAppender" /> 
</logger>

and...to pass the data from C# I use this method:

public static void writeLog(GroupType mGrupo, Hashtable mValues)
    {
        ILog Logger = oLogger.GetLoggerByGroup(mGrupo);

        Type declaringType = typeof(log4net.LogManager);
        LoggingEvent loggingEvent = new LoggingEvent(declaringType, Logger.Logger.Repository, Logger.Logger.Name, Level.All, null, null);//null = Message, Exception            

        foreach (DictionaryEntry CurrentEntry in mValues) 
        {
            loggingEvent.Properties[CurrentEntry.Key.ToString()] = CurrentEntry.Value;
        }
        Logger.Logger.Log(loggingEvent);
    }

I don't know how works the LoggingEvent, but I use it to fill other table and work perfectly. If somebody can help me with this issue, I apreciate!!

Thanks to read my bad english!!

Answer

Stefan Egli picture Stefan Egli · May 24, 2012

I am not 100% sure what you are asking. What I can tell you for sure is this: If you want to log custom properties, you need to use RawPropertyLayout and specify property name. Example:

<parameter>
   <parameterName value="@clientid"/>
   <dbType value="Int32"/>
   <layout type="log4net.Layout.RawPropertyLayout">
      <key value="clientid" />
   </layout>
</parameter>