Skip to content

API Library Log

Robert Polak edited this page Jul 25, 2019 · 12 revisions

Home / API Library / Log

API Library Log used for Logging application events. API Library Log implement the Log4Net. The API Library Log Methods list with Parameters, Return examples.

  • Details about the Apache Log4Net framework at Log4Net

Configuration Log4Net

Configuration Log4Net - Details

1. Configuration of Log variables at App.config: Log4Net .

<configuration>
	</configSections>
		<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
	</configSections>
	
        <log4net>
		<root>
			<!-- Set the level to ERROR for Live/UAT and ALL for TD-->
			<level value="ALL"/>
			<appender-ref ref="FileAppender"/>
			<appender-ref ref="SmtpAppender"/>
			<appender-ref ref="AdoNetAppender"/>
		</root>
	...
	</log4net>
</configuration>

2. Configuration of Log variables at App.config: Log4Net - appender - FileAppender.

<configuration>
        <log4net>
		...
		<appender name="FileAppender" type="log4net.Appender.FileAppender">
			<!-- Set threshold to ERROR for Live/UAT and ALL for TD-->
			<threshold value="ALL"/>
			<file type="log4net.Util.PatternString" value="Logs\Log4Net.[%processid].log"/>
			<appendToFile value="true"/>
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date [%thread] %level %class.%method:%line - %message%newline"/>
			</layout>
			<lockingModel type="log4net.Appender.FileAppender+InterProcessLock" />
		</appender>

		</log4net>
</configuration>

3. Configuration of Log variables at App.config: Log4Net - appender - SmtpAppender.

<configuration>
        <log4net>
		...

		<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
			<!-- Set threshold to ERROR for Live/UAT and OFF for TD-->
			<threshold value="OFF" />
			<!-- List of coma separated emails for the recipients -->
			<to value="" />
			<!-- Email of the sender -->
			<from value="" />
			<!-- Email's subject -->
			<subject type="log4net.Util.PatternString" value="Domain [Environment] - Error log" />
			<!-- SMTP IP address -->
			<smtpHost value="" />
			<!-- SMTP IP port -->
			<port value="25" />
			<authentication value="Basic" />
			<bufferSize value="1" />
			<lossy value="true" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date [%thread] %level %class.%method:%line - %message%newline"/>
			</layout>
		</appender>

        </log4net>
</configuration>

4. Configuration of Log variables at App.config: Log4Net - appender - AdoNetAppender.

<configuration>
        <log4net>
		...

		<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
			<!-- Set threshold to ERROR for Live/UAT and ALL for TD-->
			<threshold value="ALL" />
			<bufferSize value="1" />
			<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
			<connectionString value="Server=;Initial Catalog=;User ID=;Password=;Persist Security Info=False;Column Encryption Setting=enabled;" />
			<commandText value="INSERT INTO TD_LOGGING ([LGG_DATETIME],[LGG_THREAD],[LGG_LEVEL],[LGG_CLASS],[LGG_METHOD],[LGG_LINE],[LGG_MESSAGE],[LGG_EXCEPTION]) VALUES (@Datetime,@Thread,@Level,@Class,@Method,@Line,@Message,@Exception)" />
			<parameter>
				<parameterName value="@Datetime" />
				<dbType value="DateTime" />
				<layout type="log4net.Layout.RawTimeStampLayout" />
			</parameter>
			<parameter>
				<parameterName value="@Thread" />
				<dbType value="String" />
				<size value="8" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%thread" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@Level" />
				<dbType value="String" />
				<size value="8" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%level" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@Class" />
				<dbType value="String" />
				<size value="256" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%class" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@Method" />
				<dbType value="String" />
				<size value="256" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%method" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@Line" />
				<dbType value="String" />
				<size value="8" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%line" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@Message" />
				<dbType value="String" />
				<size value="-1" />
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%message" />
				</layout>
			</parameter>
			<parameter>
				<parameterName value="@Exception" />
				<dbType value="String" />
				<size value="-1" />
				<layout type="log4net.Layout.ExceptionLayout" />
			</parameter>
		</appender>
        </log4net>
</configuration>

Levels of Logging

Logging code Logging Level Scope
Log.Instance.Info Info API Library
Log.Instance.Fatal Fatal API Library
Log.Instance.Debug Debug API Application
Log.Instance.Error Error API Application

Threshold of Logging

Debug TD UAT LIVE
File (FileAppender) ALL ALL ERROR ERROR
SMTP (SmtpAppender) OFF OFF ERROR ERROR
ADO (AdoNetAppender) ALL ALL ERROR ERROR

API Library Log - Usage examples

   Log.Instance.Info("SQL Server Connection Name: " + connectionName);
   Log.Instance.Debug("No data found");
   Log.Instance.Fatal("Fatal error found"); 
   Log.Instance.Error("Error found"); 

API Library Log - appender Logging to Database ConfigurationOK

API Library Log Logging event can be written to Database as see Configuration of Log variables at App.config section <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">. To use this appender configuration is required to run script on database to create TD_LOGGING table as listed below:

CREATE TABLE [dbo].[TD_LOGGING](
	[LGG_ID] [bigint] IDENTITY(1,1) NOT NULL,
	[LGG_DATETIME] [datetime] NOT NULL,
	[LGG_THREAD] [varchar](8) NOT NULL,
	[LGG_LEVEL] [varchar](8) NOT NULL,
	[LGG_CLASS] [varchar](256) NULL,
	[LGG_METHOD] [varchar](256) NULL,
	[LGG_LINE] [varchar](8) NULL,
	[LGG_MESSAGE] [varchar](max) NULL,
	[LGG_EXCEPTION] [varchar](max) NULL,
 CONSTRAINT [PK_TD_LOGGING] PRIMARY KEY CLUSTERED 
(
	[LGG_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Instance

Instance - Details

Send an Log by using the default template. Returns void

Method: API.Log.Instance

Parameters: N/A

Name Type Default Description
template String Properties.Resources.Log default template

Return: ILog

Clone this wiki locally