log4net错误:当F#脚本调用C#可执行文件时发生。

3

我有一个写log4net日志的C#可执行文件。如果我直接运行该可执行文件,记录日志可以正常工作。 然而,如果我从一个F#脚本(扩展名为fsx)中调用该可执行文件,则会出现错误。

log4net:ERROR Failed to find configuration section 'log4net' in the application'
s .config file. Check your .config file for the <log4net> and <configSections> e
lements. The configuration section should look like: <section name="log4net" typ
e="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

有人可以帮忙吗?非常感谢。

你能在你的问题中包含调用C#可执行文件的F#代码吗? - Ryan Gates
2个回答

2

您需要将此exe文件的配置插入到您的F#应用程序的配置文件中,或在代码中初始化记录器。我不确定当您运行F#脚本时是否有配置文件。

您可以尝试使用以下代码将配置设置为C# exe的configfile:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "c:/test.config);

F# 脚本由 F# Interactive (fsi.exe) 运行。 - kvb
谢谢,我知道。你可以尝试使用AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "c:/test.config")将配置设置到C#应用程序的配置文件中。 - Kirill Bestemyanov
我只是想说,F#脚本的相关配置文件将是fsi.exe.config。 - kvb
我不确定这会不会起作用... 尝试按照我写的代码配置来设置。 - Kirill Bestemyanov
发现当从另一段代码调用代码片段时,您不能仅依赖默认或当前目录。 - hong pei

1
当您从F# Interactive调用C# .exe时,当前工作目录(.NET将尝试从中加载.config文件的位置)是安装F# Interactive的目录。
在您的F#交互式脚本中,可以这样做:
// Change the current directory to the directory where the
// C# assembly was loaded from.
System.Environment.CurrentDirectory <-
    // TODO : Change 'MyType' to any public type from your C# assembly
    typeof<FSharpx.Text.Lexing.Position>.Assembly.Location

编辑: 仔细想想,我不确定上面的代码是否有效 -- 它假设 .config 文件将被惰性加载(即按需加载)。如果 CLR 在加载 .exe 的同时加载 .config 文件,则需要改为执行以下操作:

// Change the current directory *before* referencing the C# assembly.
System.Environment.CurrentDirectory <- @"C:\blah";;

// Reference the C# assembly
#r @"C:\blah\foobar.exe";;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接