[英].Net Core Worker Service cannot find log4net.config when it runs as a windows service
I have these projects in my solution:我的解决方案中有这些项目:
But my log4net.config
file is inside my worker service project.但是我的log4net.config
文件在我的工作服务项目中。 When I'm debugging, it is logging to the file which is configured in the log4net.config
.当我调试时,它会记录到log4net.config
配置的文件。
But when I install the Worker Service as a Windows service, it doesn't log to the file.但是当我将 Worker Service 安装为 Windows 服务时,它不会记录到文件中。
And when I checked the log4net.config
location it has these values:当我检查log4net.config
位置时,它具有以下值:
D:\\myrepos\\webapicore\\Development\\Message.Consumer\\log4net.config
调试时: D:\\myrepos\\webapicore\\Development\\Message.Consumer\\log4net.config
C:\\WINDOWS\\system32\\log4net.config
作为服务运行时: C:\\WINDOWS\\system32\\log4net.config
So I believe, since the log4net.config
is not available in C:\\WINDOWS\\system32\\
it cannot do the logging.所以我相信,由于log4net.config
在C:\\WINDOWS\\system32\\
中不可用,因此无法进行日志记录。
UPDATE: I'm using following code to retrieve the log4net.config更新:我使用以下代码来检索 log4net.config
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
Any idea to solve this?有什么想法可以解决这个问题吗?
Thanks.谢谢。
I have installed and configured log4net in a different class library.我已经在不同的类库中安装并配置了 log4net。 So when I was getting the Assembly.GetEntryAssembly()
it was actually not giving the assembly location of where log4net has been installed.因此,当我获取Assembly.GetEntryAssembly()
它实际上并未提供安装 log4net 的位置的程序集位置。
Therefore I changed the this code part:因此我更改了此代码部分:
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
Into:进入:
var dirname = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
XmlConfigurator.Configure(new FileInfo(string.Format("{0}{1}", dirname, @"\log4net.config")));
This worked for me.这对我有用。
This worked for me too with my .NET 5 WorkerService
.这也适用于我的 .NET 5 WorkerService
。
You could also use AppDomain.CurrentDomain.BaseDirectory
, instead.您也可以改用AppDomain.CurrentDomain.BaseDirectory
。
However, I then found out that you can simply create an AssemblyInfo.cs
file (Add new item ...) and add但是,我随后发现您可以简单地创建一个AssemblyInfo.cs
文件(添加新项目...)并添加
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Just like before with .NET Framework 4.5, etc.就像以前使用 .NET Framework 4.5 等一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.