C# 使 NLog.config 文件从 (d:\dev) 而不是 "\bin\debug\" 加载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15958271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:36:31  来源:igfitidea点击:

make NLog.config file load the file from (d:\dev) instead of "\bin\debug\"

c#configurationconfiguration-filesnlognlog-configuration

提问by rookie_developer

I used Nlog for logging purpose in a particular DLL. The DLL is then used in another application (it is loaded dynamically using System.Reflection.Assembly.LoadFrom(path + a.dll)). I manually placed Nlog.dll and Nlog.config files in Path folder and the application executes properly but it does not log any messages.

我在特定的 DLL 中使用 Nlog 进行日志记录。然后在另一个应用程序中使用 DLL(它使用 动态加载System.Reflection.Assembly.LoadFrom(path + a.dll))。我手动将 Nlog.dll 和 Nlog.config 文件放在 Path 文件夹中,应用程序正常执行,但不记录任何消息。

However, when I go ahead and place the Nlog.config file manually in application directory (\bin\debug\) is logs messages.

但是,当我继续将 Nlog.config 文件手动放置在应用程序目录 ( \bin\debug\) 中时,是日志消息。

Can someone let me know how to point the search location for Nlog.Config to a different directory (d:\dev) other than \bin\debug\.

有人可以让我知道如何将 Nlog.Config 的搜索位置指向不同的目录 ( d:\dev),而不是\bin\debug\.

采纳答案by rookie_developer

Below is how i changed configuration of Nlog to point to Nlog.config file present in Executing Assembly's folder.

下面是我如何更改 Nlog 的配置以指向 Executing Assembly 文件夹中的 Nlog.config 文件。

string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\NLog.config", true);

回答by Jim Sowers

The NLog config needs to reside in the folder where the app that is dynamically pulling a.dll is running from. If you are debugging, that is why it is works when you put it into bin\debug. If you are using Visual Studio, try setting your nlog.config to 'Copy Always' and it should go where you need it.

NLog 配置需要驻留在正在运行动态拉取 a.dll 的应用程序所在的文件夹中。如果您正在调试,这就是将它放入 bin\debug 时它起作用的原因。如果您使用的是 Visual Studio,请尝试将您的 nlog.config 设置为“始终复制”,它应该会出现在您需要的地方。

回答by Xharze

See Configuration file locationson the NLog wiki.

请参阅NLog wiki 上的配置文件位置

Basically the ways NLog locates the config is:

基本上 NLog 定位配置的方式是:

  • standard application configuration file (usually applicationname.exe.config)
  • applicationname.exe.nlog in application's directory
  • NLog.config in application's directory
  • NLog.dll.nlog in a directory where NLog.dll is located (only if NLog is not in the GAC)
  • file name pointed by the NLOG_GLOBAL_CONFIG_FILE environment variable (if defined, NLog 1.0 only - support removed in NLog 2.0)
  • 标准应用程序配置文件(通常是applicationname.exe.config)
  • 应用程序目录中的 applicationname.exe.nlog
  • 应用程序目录中的 NLog.config
  • NLog.dll 所在目录中的 NLog.dll.nlog(仅当 NLog 不在 GAC 中时)
  • NLOG_GLOBAL_CONFIG_FILE 环境变量指向的文件名(如果定义,仅 NLog 1.0 - NLog 2.0 中删除了支持)

There are no other way to do this.

没有其他方法可以做到这一点。

回答by camt

I found that

我找到

NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(logFilePath, true);

actually points the logger to the file which is to be logged to, not the config file. The great thing about this, is that you can define the log file path without needing to know the ExecutingAssembly - this is especially useful when using ExcelDNA etc as the XLL loads the assemblies dynamically as a bitstream, and so

实际上将记录器指向要记录的文件,而不是配置文件。这样做的好处是,您无需知道 ExecutingAssembly 即可定义日志文件路径 - 这在使用 ExcelDNA 等时特别有用,因为 XLL 将程序集作为比特流动态加载,等等

Assembly.GetExecutingAssembly().Location

throws an exception.

抛出异常。

回答by Rolf Kristensen

You can use include files with NLog.config. Having a simple NLog.config that only include an NLog.config from D:\DEV.

您可以将包含文件与 NLog.config 一起使用。有一个简单的 NLog.config,它只包含一个来自D:\DEV.

Ex:

前任:

<nlog>
    <include file="D:\DEV\NLog.Config" />
</nlog>

You can also do it with app.config. Ex:

您也可以使用 app.config 来完成。前任:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
    </configSections>
    <nlog>
        <include file="D:\DEV\NLog.Config" />
    </nlog>
</configuration>

See also: https://github.com/nlog/nlog/wiki/Configuration-file#include-files

另见:https: //github.com/nlog/nlog/wiki/Configuration-file#include-files