Skip to content
DUONG Phu-Hiep edited this page Feb 25, 2019 · 7 revisions

AssemblyInfo.cs

// Configure log4net
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

or

// Configure log4net
[assembly: log4net.Config.XmlConfigurator(ConfigFile="log4net.config", Watch = true)]

or

string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\Config\\Log4Net.config";
FileInfo finfo = new FileInfo(logFilePath);
log4net.Config.XmlConfigurator.ConfigureAndWatch(finfo);

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <!-- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> -->
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
    </configSections>
 
    <appSettings>
        <add key="ApplicationName" value="HelloWorld"/>
    </appSettings>
 
    <log4net>
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value=".\Logs\SendTibco.log" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="'.'yyyy-MM-dd" />
            <layout type="log4net.Layout.PatternLayout">
                <!--<conversionPattern value="%d %-5level - %message  [%logger{1}:%L]%newline" />-->
                <conversionPattern value="%d %-5level - %message  [%logger{1}]%newline" />
            </layout>
        </appender>
        <appender name="Console" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%-5level - %message  [%logger{1}]%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingFileAppender" />
            <appender-ref ref="Console" />
        </root>
    </log4net>
 
</configuration>

Usage

private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

Colored Console

<?xml version="1.0" encoding="utf-8"?>
 
<log4net>
 
    <!-- BEGIN: Console Appender-->
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
        <mapping>
            <level value="FATAL" />
            <foreColor value="Red, HighIntensity" />
        </mapping>
        <mapping>
            <level value="ERROR" />
            <foreColor value="Red, HighIntensity" />
        </mapping>
        <mapping>
            <level value="WARN" />
            <foreColor value="Green" />
        </mapping>
        <mapping>
            <level value="DEBUG" />
            <foreColor value="Yellow" />
        </mapping>
        <mapping>
            <level value="INFO" />
            <foreColor value="Cyan" />
        </mapping>
        <layout type="log4net.Layout.PatternLayout">
            <!--<conversionPattern value="%date{yyyy/MM/dd HH:mm:ss,fff} %-5level %message [%logger{1} (%thread)]%newline" />-->
            <conversionPattern value="%-5level - %message  [%logger{1}]%newline" />
        </layout>
    </appender>
    <!-- END: Console Appender-->
 
    <root>
        <level value="INFO" />
        <appender-ref ref="ColoredConsoleAppender" />
    </root>
</log4net>

Log Viewer

UDP/TCP

UDP

<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
  <!-- <localPort value="8080" /> -->
  <param name="RemoteAddress" value="127.0.0.2" />
  <param name="RemotePort" value="877" />
  <layout type="log4net.Layout.XmlLayout">
    <locationInfo value="true" />
  </layout>
</appender>

The UDP appender in log4net 1.2.10.0 won't work for 127.0.0.1 bacause of the additional support of the IPv6 protocol, replace it by 127.0.0.2.

UDP is a “lightweight” protocol, which does not ensure, the UDP receiving of all datagrams, which are sent from the UDP appender. Normally, this isn't a problem, but in situations, where 1000 and more messages per second are transmitted, messages can get lost.

TCP

<appender name="TcpAppender1" type="prosa.LogAppender.TcpAppender, prosa.LogAppender">
  <param name="LocalPort" value="900" />
  <layout type="log4net.Layout.XmlLayout" />
</appender>

Async appender

[assembly: log4net.Config.XmlConfigurator(ConfigFile="log4net.config", Watch = true)]

package.config

<package id="Log4Net.Async" version="2.0.0.0" targetFramework="net45" />

log4net.config

<log4net>
		<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
			<file value=".\Logs\ReconciliationHelper.log" />
			<appendToFile value="true" />
			<rollingStyle value="Date" />
			<datePattern value="'.'yyyy-MM-dd" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%d %-5level - %message  [%logger{1}:%L]%newline" />
				<!--<conversionPattern value="%d %-5level - %message  [%logger{1}]%newline" />-->
			</layout>
		</appender>
		<appender name="AsyncRollingFileAppender" type="Log4Net.Async.AsyncForwardingAppender,Log4Net.Async">
			<appender-ref ref="RollingFileAppender" />
		</appender>
		<root>
			<level value="DEBUG" />
			<appender-ref ref="AsyncRollingFileAppender" />
		</root>
	</log4net>