Winforms log4net.Config.XmlConfigurator.Configure()给出-“;FileNotFoundException跨越了本机/托管边界“;

Winforms log4net.Config.XmlConfigurator.Configure()给出-“;FileNotFoundException跨越了本机/托管边界“;,winforms,log4net,app-config,filenotfoundexception,boundary,Winforms,Log4net,App Config,Filenotfoundexception,Boundary,这只是我第二次尝试使用log4net。这是一个Windows窗体应用程序,我以为一切都设置好了,但我没有创建任何日志文件。我发现了一些建议,建议需要显式地让它读取app.config文件,因此我在这里添加了log4net.config.XmlConfigurator.Configure(): public partial class GridForm : Form { public static readonly ILog log = LogManager.GetLogger(typeo

这只是我第二次尝试使用log4net。这是一个Windows窗体应用程序,我以为一切都设置好了,但我没有创建任何日志文件。我发现了一些建议,建议需要显式地让它读取app.config文件,因此我在这里添加了log4net.config.XmlConfigurator.Configure()

public partial class GridForm : Form
{
    public static readonly ILog log = LogManager.GetLogger(typeof(GridForm));

    public GridForm()
    {
        InitializeComponent();
                                          // Initialize log4net (make him read the app.config file)
        log4net.Config.XmlConfigurator.Configure();     // this gives the exception 
    }
我对如何引用log4net.dll有点不确定

  • 我在解决方案中有两个项目——CBMI.Common(类库项目)和CBMI.WinFormsUI(表单项目)(启动表单)
  • 我将log4net.dll复制到了CBMI.Common的bin目录(不是bin\debug文件夹)
  • 然后在我的表单项目中添加了一个指向该位置的引用:CBMI.Common\bin
这样编译不会出错。我想应该没问题,但是

我无法从引发的异常和显示的消息中确定要执行的操作:

System.IO.FileNotFoundException crossed a native/managed boundary
  Message=Could not load file or assembly 'Log4net' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=Log4net
  FusionLog==== Pre-bind state information ===
LOG: User = HPpavilion\john
LOG: DisplayName = Log4net
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Log4net | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\john\documents\visual studio 2010\Projects\CBMI.LatitudePostConverter\CBMI.WinFormsUI\bin\Debug\CBMI.WinFormsUI.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net.DLL.
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net/Log4net.DLL.
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net.EXE.
LOG: Attempting download of new URL file:///C:/Users/john/documents/visual studio 2010/Projects/CBMI.LatitudePostConverter/CBMI.WinFormsUI/bin/Debug/Log4net/Log4net.EXE.

  StackTrace:
       at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
       at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)
       at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
       at System.Type.GetType(String typeName, Boolean throwOnError)
       at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.GetConfigType(String typeName, Boolean throwOnError)
       at System.Configuration.Internal.DelegatingConfigHost.GetConfigType(String typeName, Boolean throwOnError)
       at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
  InnerException: 
为完整起见,我将添加app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="CBMI.WinFormsUI.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>

</configSections>

<userSettings>
  <CBMI.WinFormsUI.Properties.Settings>
        <setting name="DefaultRootFolder" serializeAs="String">
            <value />
        </setting>
    </CBMI.WinFormsUI.Properties.Settings>
</userSettings>

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="log-file.txt"/>
        <param name="AppendToFile" value="true"/>
        <rollingStyle value="Size"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="10MB"/>
        <staticLogFileName value="true"/>
        <layout type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
        </layout>
    </appender>
    <root>
        <level value="DEBUG"/>
        <appender-ref ref="LogFileAppender"/>
    </root>
</log4net>

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

</configuration>


我的修复方法是将
[assembly:log4net.Config.XmlConfigurator(Watch=true)]
插入assemblyinfo.cs

我的修复方法是插入
[assembly:log4net.Config.XmlConfigurator(Watch=true)]
进入assemblyinfo.cs

是否将log4net dll复制到与windows forms exe相同的目录中?感谢您注意我的问题。否,未将log4net.dll复制到我的bin\debug目录。这是应该发生的事情吗?我手动将log4net.dll复制到bin\debug,但这没有任何区别。无论何时,只要有:log4net.Config.XmlConfigurator.Configure();在我的构造函数中(如上所述),它失败了。我在GAC中将引用改为指向log4net(就像另一个正在工作的项目一样),但存在相同的问题。我的解决办法是将[assembly:log4net.Config.XmlConfigurator(Watch=true)]插入assemblyinfo.csIs log4net dll是否被复制到与windows forms exe相同的目录中?谢谢注意我的问题。否,未将log4net.dll复制到我的bin\debug目录。这是应该发生的事情吗?我手动将log4net.dll复制到bin\debug,但这没有任何区别。无论何时,只要有:log4net.Config.XmlConfigurator.Configure();在我的构造函数中(如上所述),它失败了。我在GAC中将引用改为指向log4net(就像另一个正在工作的项目一样),但存在相同的问题。我的修复方法是将[assembly:log4net.Config.XmlConfigurator(Watch=true)]插入assemblyinfo.cs