Apache log4net has various built-in appenders which allow adjustments to the way logging is handled.  DataSplice comes out of the box with the ability to write logs to a file and send email notifications when the logs have changed.  In additional to these appenders, there is also the ability to log to a database using the AdoNetAppender.


Note:  The AdoNetAppender requires a server restart for logs to be committed to a database table.  The default file logging will be the most accurate and up to date.


A. Editing the Configuration


 

  • The following changes will be made to the dssplicer.exe.config file located within DataSplice's Server folder.


    • 2016-10-20%2013_18_00-Server.png


  • All changes to the dssplicer configuration file will be done between the <log4net> ... </log4net> XML element.  
  • Below is the new appender's configuration which is added between the log4net element in the configuration file.

  

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=DATABASE_SERVER;initial catalog=DATABASE_NAME;integrated security=false;persist security info=True;User ID=USER;Password=PASSWORD" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
    <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
</appender>

  

  • Modifiy the <connectionString> attribute to match the database where the logs will be stored.

    • <connectionString value="data source=DATABASE_SERVER;initial catalog=DATABASE_NAME;integrated security=false;persist security info=True;User ID=USER;Password=PASSWORD" />
      • DATABASE_SERVER - Server where the database is stored.
      • DATABASE_NAME - Name of the database.
      • USER - Username of the user with read/write privileges.
      • PASSWORD - Password of the user.
  • After the <connectionString> attribute has been modified, reference the new appender within the <root> ... </root> element by adding the following tag:
    • <appender-ref ref="AdoNetAppender" />
  • Below is an example of a complete <root> element:

  

<root>
	<level value="INFO" />
	<appender-ref ref="TraceAppender" />
	<appender-ref ref="RollingFileAppender" />
	<appender-ref ref="AdoNetAppender" />
	<!-- uncomment this line to enable email notifications
      <appender-ref ref="SmtpAppender"/>
      -->
</root>


  • The complete dssplicer.exe.config file should look as follows:

  

<?xml version ="1.0"?>
<configuration>
	<configSections>
		<section name="pluginEvents" type="System.Configuration.IgnoreSectionHandler" />
		<section name="additionalContentTypes" type="System.Configuration.IgnoreSectionHandler" />
		<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
	</configSections>
	<startup>
		<supportedRuntime version="v2.0.50727" />
	</startup>
	<runtime>
		<enforceFIPSPolicy enabled="false"/>
	</runtime>
	<appSettings>
		<add key="ServerInstanceName" value="DEFAULT" />
		<add key="ServerPort" value="4180" />
		<add key="StorageFolder" value="Storage" />
		<add key="UseHTTPS" value="false" />
		<add key="HTTPSCertFileName" value="HTTPS-DataSpliceServer.p12" />
		<add key="HTTPSPassword" value="" />
		<add key="MaxContentSize" value="15000000" />
	</appSettings>
	<pluginEvents>
		<event name="BarcodeScan" flags="client,qualified,global"/>
		<event name="NetworkConnected" flags="client,global"/>
		<event name="NetworkDisconnected" flags="client,global"/>
		<event name="TestEvent" flags="client,qualified,global"/>
	</pluginEvents>
	<additionalContentTypes>
		<add extension="appcache" contentType="text/cache-manifest" />
		<add extension="json" contentType="application/json" />
		<add extension="jar" contentType="application/java-manifest" />
		<add extension="manifest" contentType="text/cache-manifest" />
		<add extension="svg" contentType="image/svg+xml" />
		<add extension="ttf" contentType="application/x-font-ttf" />
		<add extension="woff" contentType="application/x-font-woff" />
	</additionalContentTypes>
	<log4net>
		<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%d [%t] %-5p %c - %m%n" />
			</layout>
		</appender>
		<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
			<file value="logs/DSSplicer.Log.txt"/>
			<appendToFile value="true"/>
			<countDirection value="1"/>
			<rollingStyle value="Size" />
			<maxSizeRollBackups value="100" />
			<maximumFileSize value="100KB" />
			<staticLogFileName value="true" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%d [%t] %-5p %c - %m%n" />
			</layout>
		</appender>
		<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
			<to value="debug@datasplice.com" />
			<from value="logger@datasplice.com" />
			<subject value="An error was logged in DataSplice!" />
			<smtpHost value="mail.datasplice.com" />
			<bufferSize value="50" />
			<lossy value="true" />
			<!-- only email error or greater messages -->
			<evaluator type="log4net.spi.LevelEvaluator">
				<threshold value="ERROR" />
			</evaluator>
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%d [%t] %-5p %c - %m%n" />
			</layout>
		</appender>
		<!-- START AdoNetAppender -->
		<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
			<bufferSize value="100" />
			<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
			<connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
			<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
			<parameter>
				<parameterName value="@log_date" />
				<dbType value="DateTime" />
				<layout type="log4net.Layout.RawTimeStampLayout" />
			</parameter>
			<parameter>
				<parameterName value="@thread" />
				<dbType value="String" />
				<size value="255" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%thread" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@log_level" />
				<dbType value="String" />
				<size value="50" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%level" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@logger" />
				<dbType value="String" />
				<size value="255" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%logger" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@message" />
				<dbType value="String" />
				<size value="4000" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%message" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@exception" />
				<dbType value="String" />
				<size value="2000" />
				<layout type="log4net.Layout.ExceptionLayout" />
			</parameter>
		</appender>
		<!-- END AdoNetAppender -->
		<root>
			<level value="INFO" />
			<appender-ref ref="TraceAppender" />
			<appender-ref ref="RollingFileAppender" />
			<appender-ref ref="AdoNetAppender" />
			<!-- uncomment this line to enable email notifications
      <appender-ref ref="SmtpAppender"/>
      -->
		</root>
	</log4net>

  


    B. Creating the Database's Table


    • A new table must be created with the following columns:

    CREATE TABLE [dbo].[Log] (
        [Id] [int] IDENTITY (1, 1) NOT NULL,
        [Date] [datetime] NOT NULL,
        [Thread] [varchar] (255) NOT NULL,
        [Level] [varchar] (50) NOT NULL,
        [Logger] [varchar] (255) NOT NULL,
        [Message] [varchar] (4000) NOT NULL,
        [Exception] [varchar] (2000) NULL
    )

     


    C. Restart the Server's Service


    • Restart DataSplice's server.