开发者

log4net.Config.XmlConfigurator.Configure() gives - "FileNotFoundException crossed a native/managed boundary"

This is only the 2nd time I've tried to use log4net. This is a Windows form app and I thought things are set up OK but I was not getting any logfile created. I found some advice about needing to explicitly make it read the app.config file so I added log4net.Config.XmlConfigurator.Configure() here:

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 
    }

I have a little uncertainty about how I'm referencing the log4net.dll.

  • I have 2 projects in the solution - CBMI.Common which is a class library project and CBMI.WinFormsUI which is my forms project (startup form).
  • I copied log4net.dll to the bin directory of CBMI.Common (NOT the bin\debug folder)
  • then added a reference in my forms project that points to that location: CBMI.Common\bin

This compiles without error. I think that it should be OK, but...

I cannot determine what to do from the exception raised and messages shown:

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开发者_StackOverflow社区.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
  InnerException: 

For completeness, I will add the app.config file:

<?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>


The fix for me was to insert [assembly: log4net.Config.XmlConfigurator(Watch = true)] into assemblyinfo.cs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜