First, set Nuget.
Tools → → Nuget Package Manager Package Manager disposed → NuGet Package Manager → → check package source nuget.org https://www.nuget.org/api/v2/→ determined.
Second, add the log4net Nuget package.
NuGet package Tools → Nuget → Package Manager management solutions
Third, add a profile
Right-click the project → Add → New Item → application configuration file (web or winform ......) and modify the name: value log4net.config → Right-click generated out of the log4net.config → Properties → "Copy to Output Directory" of change as if newer copy.
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<!-- Set root logger level to ERROR and its appenders -->
<!--<root>
<level value="ALL" />
<appender-ref ref="SysAppender" />
</root>
-->
<logger name="LogError">
<level value="ERROR" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
<logger name="LogInfo">
<level value="INFO" />
<appender-ref ref="InfoAppender" />
</logger>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log\\LogError\\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=""Logs_"yyyyMMdd".txt"" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
<!-- <param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />-->
</layout>
</appender>
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\LogInfo\\" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
</configuration>
a message output from the message log, as ILog.Debug (...) output:% m (Message)
% n-(new new Line): linefeed
% d (datetime): output time of the current statement running
% r (run time): Output program is consumed from the operation to execute the current statement is the number of milliseconds
% t (thread id): current statement where the thread ID
% the p-(priority): the current priority level from which logs that DEBUG, INFO, WARN ... and so
% c (class ): the current name of the log object, for example:
% f (file): filename where output statements.
% l (line): the line number where the output statement.
% Number: indicates that the minimum length, if not, then padded with spaces, such as "% -5level" represents the minimum width level is five characters long, if the actual length of the spaces not filled places five characters.
{
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("LogInfo");
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("LogError");
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
{
if (logerror.IsErrorEnabled)
{
logerror.Error(errorMsg, ex);
}
}
}
{
spReceive1.Open ();
}
the catch (Exception E)
{
Logger.ErrorLog ( "open the scanning head 1", E);
MessageBox.Show ( "scan head 1 open failed!");
}