简体   繁体   English

.Net Core Worker Service 作为 Windows 服务运行时找不到 log4net.config

[英].Net Core Worker Service cannot find log4net.config when it runs as a windows service

I have these projects in my solution:我的解决方案中有这些项目:

  1. .Net Core 3.1 Worker Service .Net Core 3.1 工作者服务
  2. .Net Core 3.1 Class library - for configure the log4net .Net Core 3.1 类库 - 用于配置 log4net

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位置时,它具有以下值:

  1. When debugging: D:\\myrepos\\webapicore\\Development\\Message.Consumer\\log4net.config调试时: D:\\myrepos\\webapicore\\Development\\Message.Consumer\\log4net.config
  2. When running as a service: 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.configC:\\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.

相关问题 无法 dockerize 在 OS X 上运行的 .net core 3.1 工作服务 - Cannot dockerize a .net core 3.1 worker service that runs on OS X .Net Core 6 Worker Service 在作为 Windows 服务运行时不写入日志 - .Net Core 6 Worker Service not writing to Logs when running as a Windows Service 如何使用log4net.config文件更改服务的日志记录级别? - How to change the Logging Level of a service using the log4net.config file ? 在 .net 核心工作者服务中获取登录用户 - Get log on user in .net core worker service 将 log4net.config 移动到非标准位置的 dot net Core 中时如何修复错误(不是解决方案的根目录) - How to fix error when moving log4net.config in a dot net Core in a non standard place (not the root of the solution) 1053 Windows 服务错误使用 .NET Core 3.1 Worker 服务 - 1053 Windows Service Error using .NET Core 3.1 Worker Service 安装 .NET Core 3.0 Worker Service 和 WiX 作为 Windows 服务 - Install .NET Core 3.0 Worker Service with WiX as Windows Service ASP零:System.IO.FileNotFoundException:找不到文件'c:\\ windows \\ system32 \\ inetsrv \\ log4net.config' - ASP Zero :System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\log4net.config' .NET Core Worker Service 作为 Windows 服务在调用 StopAsync 后未停止 - .NET Core Worker Service as Windows service not stopping after calling StopAsync ASP.Net Core 2.1 应用程序在作为 Windows 服务运行时找不到 appsettings.json - ASP.Net Core 2.1 application cannot find appsettings.json when ran as a Windows service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM