[STAThread]
static void Main(string[] args)
{
string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
//string configFilePath = Path.Combine(assemblyDirPath, "HelperToolslog4net.config");
string configFilePath = Path.Combine(assemblyDirPath, "log4net.config");
//DOMConfigurator.Configure(new FileInfo(configFilePath));
log4net.Config.XmlConfigurator.Configure(new FileInfo(configFilePath));
}
public static ILog GetLoggerByName(string name)
{
if (LogManager.Exists(name) == null)
{
// Pattern Layout defined
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date %thread %level %logger - %message%newline";
patternLayout.ActivateOptions();
// configurating the RollingFileAppender object
RollingFileAppender appender = new RollingFileAppender();
appender.Name = name;
appender.AppendToFile = true;
appender.File = $"Logs\\{name}.log";
appender.StaticLogFileName = true;
appender.PreserveLogFileNameExtension = true;
appender.LockingModel = new FileAppender.MinimalLock();
appender.Layout = patternLayout;
appender.MaxSizeRollBackups = 512;
appender.MaximumFileSize = "150MB";
appender.RollingStyle = RollingFileAppender.RollingMode.Composite;
appender.ActivateOptions();
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
var loger = hierarchy.GetLogger(name, hierarchy.LoggerFactory); //!!! 此处写法是重点,不容更改
loger.Hierarchy = hierarchy;
loger.AddAppender(appender);
loger.Level = Level.All;
BasicConfigurator.Configure();//!!! 此处写法是重点,不容更改
var appname = Assembly.GetEntryAssembly().GetName().Name;
var version = Assembly.GetEntryAssembly().GetName().Version;
loger.Log(Level.Info, $"Log name {name} created for Application: {appname} Version: {version}", null);
}
var log = LogManager.GetLogger(name);
return log;
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<logger name="LogInfo">
<level value="ALL" />
<appender-ref ref="LogInfoAppender" />
</logger>
<appender name="LogInfoAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Log\.txt" />
<param name="Encoding" value="utf-8" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Composite" />
<param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="StaticLogFileName" value="false" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaximumFileSize" value="10MB" />
<param name="PreserveLogFileNameExtension" value="true"/>
<param name="CountDirection" value="1" />
<datePattern value="yyyy-MM-dd" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d:%-5level [%t](%C:%M:%L) - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ALL" />
<param name="LevelMax" value="OFF" />
</filter>
</appender>
</log4net>
</configuration>