The original project with the Log4net, but the version is older, in the core of the new version is also supported, this article is now the latest release 2.0.8
1. Loghelper Help class put another class library in Log/loghelper.cs
2, the separate configuration file is placed under the main project, Logconfig/log4net.config
Specific locations such as:
LogHelper.cs Content
Upgrade version after the original Logmanager.getlogger () method to specify repository see the article is to createrepository one.
private static readonly Iloggerrepository Repository = Logmanager.createrepository (assembly.getentryassembly (), typeof (Hierarchy))
However, I do not write this way, no error log will not be written. Back study half-day with getrepository solution.
private static readonly Iloggerrepository Repository = Logmanager.getrepository (assembly.getcallingassembly ());
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Collections.Concurrent;usingSystem.IO;usingSystem.Reflection;usinglog4net;usinglog4net. Config;usinglog4net. Repository;usinglog4net. Repository.hierarchy;//specify config file used by log4net to read configuration information[Assembly:xmlconfigurator (ConfigFile =@"Logconfig\log4net.config", Watch =true)]namespacec.until{/// <summary> ///Log Helper Class/// </summary> Public classLoghelper {#regionOlder versions//private static readonly ILog Loginfo = Logmanager.getlogger ("Loginfo"); //private static readonly ILog LogError = Logmanager.getlogger ("LogError"); #endregion Private Static ReadOnlyIloggerrepository Repository =logmanager.getrepository (assembly.getcallingassembly ()); Private Static ReadOnlyILog loginfo = Logmanager.getlogger (Repository.name,"Loginfo"); Private Static ReadOnlyILog LogError = Logmanager.getlogger (Repository.name,"Logerror"); Public Static voidError (stringErrorMsg, Exception ex =NULL) { if(Ex! =NULL) {logerror.error (errormsg, ex); } Else{logerror.error (errormsg); } } Public Static voidInfo (stringmsg) {Loginfo.info (msg); } }}
Log4net.config
<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <!--Add a custom node: log4net type: Resolves the class name, assembly name (Log4net.dll) and <sec tion Name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler, Log4net"/> </configSections> <log4net> <appender name="Log4net_info"Type="log4net. Appender.rollingfileappender"> <!--definition file storage location-<file value="log\\loginfo\\"/> <encoding value="Utf-8"/> <!--append to file, default to True, usually without setting--<appendtofile value="true"/> <rollingstyle value="Date"/> <!--date format, change every day for a file record, if not set then always log only one day, need to set--<datepattern value=""log"yyyymm". log""/> <!--log file name is static--<staticlogfilename value="false"/> <!--multi-threading with minimal lock-to-<lockingmodel type="log4net. Appender.fileappender+minimallock"/> <!--layout (show users the last formatted output information)-<layout type="log4net. Layout.patternlayout"> <conversionpattern value="recording time:%date%n error Description:%message%newline%n"/> </layout> </appender> <appender name="Log4net_error"Type="log4net. Appender.rollingfileappender"> <!--definition file storage location-<file value="log\\logerror\\"/> <encoding value="Utf-8"/> <!--append to file, default to True, usually without setting--<appendtofile value="true"/> <rollingstyle value="Date"/> <!--date format, change every day for a file record, if not set then always log only one day, need to set--<datepattern value=""log"yyyymm". log""/> <!--log file name is static--<staticlogfilename value="false"/> <!--multi-threading with minimal lock-to-<lockingmodel type="log4net. Appender.fileappender+minimallock"/> <!--layout (show users the last formatted output information)-<layout type="log4net. Layout.patternlayout"> <conversionpattern value="recording time:%date%n error Description:%message%newline%n"/> </layout> </appender> <logger name="Logerror"> <level value="ERROR"/> <appender-ref ref="Log4net_error"/> </logger> <logger name="Loginfo"> <level value="INFO"/> <appender-ref ref="Log4net_info"/> </logger> </log4net></configuration>
This is because different messages are placed in different file classification processing "Ogger name=" Logerror "this property.
PS: In the LogHelper.cs file, use [Assembly:xmlconfigurator (configfile = @ "Logconfig\log4net.config", Watch = True)] to read the configuration file, You can also register the file path in Startup.cs. However, this method is not useful for porting to other projects for independence.
Log4net go from MVC to Netcore