.Net Core Worker Service 作为 Windows 服务运行时找不到 log4net.config [英] .Net Core Worker Service cannot find log4net.config when it runs as a windows service

查看:31
本文介绍了.Net Core Worker Service 作为 Windows 服务运行时找不到 log4net.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有这些项目:

I have these projects in my solution:

  1. .Net Core 3.1 Worker 服务
  2. .Net Core 3.1 类库 - 用于配置 log4net

但是我的 log4net.config 文件在我的工作服务项目中.当我调试时,它会记录到 log4net.config 中配置的文件.

But my log4net.config file is inside my worker service project. When I'm debugging, it is logging to the file which is configured in the log4net.config.

但是当我将 Worker Service 安装为 Windows 服务时,它不会记录到文件中.

But when I install the Worker Service as a Windows service, it doesn't log to the file.

当我检查 log4net.config 位置时,它具有以下值:

And when I checked the log4net.config location it has these values:

  1. 调试时:D:\myrepos\webapicore\Development\Message.Consumer\log4net.config
  2. 作为服务运行时:C:\WINDOWS\system32\log4net.config

所以我相信,由于 log4net.configC:\WINDOWS\system32\ 中不可用,它无法进行日志记录.

So I believe, since the log4net.config is not available in C:\WINDOWS\system32\ it cannot do the logging.

更新:我正在使用以下代码来检索 log4net.config

UPDATE: I'm using following code to retrieve the log4net.config

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

有什么办法可以解决这个问题吗?

Any idea to solve this?

谢谢.

推荐答案

我已经在不同的类库中安装并配置了 log4net.因此,当我获得 Assembly.GetEntryAssembly() 时,它实际上没有提供 log4net 安装位置的程序集位置.

I have installed and configured log4net in a different class library. So when I was getting the Assembly.GetEntryAssembly() it was actually not giving the assembly location of where log4net has been installed.

因此我更改了此代码部分:

Therefore I changed the this code part:

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

进入:

var dirname = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
XmlConfigurator.Configure(new FileInfo(string.Format("{0}{1}", dirname, @"\log4net.config")));

这对我有用.

这篇关于.Net Core Worker Service 作为 Windows 服务运行时找不到 log4net.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆