我正在尝试将日志文件写入AppData位置,但是它不起作用。我已经尝试过env%{AppData}
,env%{APPDATA}
,%APPDATA%
,${AppData}
,${APPDATA}
,但是没有人可以使用。如果我使用C:\Temp
,它确实可以工作。
我使用Asp.Net Core 2.2。和log4net 2.08。
有什么建议吗?
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date{dd-mm-yyyy HH:mm:ss} %logger [%thread] - %message %exception%newline" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="${AppData}\MyApp\MyLogFile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date{dd-mm-yyyy HH:mm:ss} %logger [%thread] - %message %exception%newline" />
</layout>
</appender>
</log4net>
答案 0 :(得分:0)
对于遇到同样问题的任何人
Nuget(安装):
log4net
Microsoft.Extensions.Options.ConfigurationExtensions
Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Logging.Debug
Microsoft.Extensions.Logging.Log4Net.AspNetCore
AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
appsettings.json:
"Log4NetConfigFile": {
"Name": "log4net.config"
},
Program.cs
using Microsoft.Extensions.Logging; //Had to write manually
public static void Main(string[] args)
{
var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
log4net.GlobalContext.Properties["LogPath"] = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApp");
log4net.Config.XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((webHostBuilderContext, loggingBuilder) =>
{
loggingBuilder.AddLog4Net();
})
.UseStartup<Startup>();
}
将log4net.config设置为复制到输出目录:始终复制
log4net.config:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date{dd-mm-yyyy HH:mm:ss} %logger [%thread] - %message %exception%newline" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogPath}\MyLogFile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date{dd-mm-yyyy HH:mm:ss} %logger [%thread] - %message %exception%newline" />
</layout>
</appender>
</log4net>
HomeController.cs
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController));
public HomeController()
{
log.Info("Starting GenerateDebugLog method...");
}