質問

I am developing an AutoCAD 2012 plugin using .net API"s of autocad. The Autocad plugin's are dll which are loaded into the Autocad runtime when the Autocad is started. I have been able to successfully log from log4net using Visual Studio developement environment. However, when I run the plugin outside of Visual Studio, that is when I deploy my plugin, the log4net does not log any messages or even create a log file. Here is the logging related code and the configuration file.

log4net_autocad.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
  </configSections>

  <log4net>
    <appender name="AutocadRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="${NIRVANA_SOFTWARE_INSTALL_PATH}Relay_Manager_Autocad.log"/>
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="0" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p [%C.%M] %m%n"/>
      </layout>
    </appender>
    <logger name="AutocadFile">
      <level value="All"/>
      <appender-ref ref="AutocadRollingFileAppender"/>
    </logger>
  </log4net>
</configuration>

myPlugin.cs: Code to congifure logging

public class MyPlugin : IExtensionApplication {

    private static log4net.ILog log;

    void IExtensionApplication.Initialize() {
        ...
        log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net_autocad.config"));
        log = log4net.LogManager.GetLogger("AutocadFile");
    }

void IExtensionApplication.Initialize() will execute when the Autocad is started. Couple of things that I have checked:

  • Checked that the Autocad Plugin is properly loaded in Autocad, which means that log4net initialization code has run.
  • Checked that log4net config file (log4net_autocad.config) is copied in the bin directory of the deployed application

The strange thing is that when I load the plugin in developement environment(VS 2010), the plugin logs properly. So why is the log4net not logging (or even creating a log file) in deployment.

EDIT: I checked some additional stuff

In the plugin just before I log, I checked following through alert dialogs.

  • The log.Logger.Name properly returns the name of the logger that I am using.
  • log.Logger.IsEnabledFor(log4net.Core.Level.All) returns false, even though I have configured Level Value=All in the config file.



EDIT-2 I tried to define config file as explained in the referring url of the answer. I am quoting the approach as outlined in the document.

2.] Application Configuration File: [AppName].exe.config, [AppName].dll.config The application-level configuration, [AppName].exe.config or [AppName].dll.config, is where most of this document will spend its time. This configuration file will contain all applicationlevel settings and can even be used to define the use of and default values for settings associated with the Roaming-User and Local-User configuration files. These files are typically stored in the same directory as the application executable, but can be placed elsewhere if necessary. There will be much more discussion on this to come.

The above approach suits my application, but I still can't get the logger to log anything at all.I defined a log4net config file with name RelayAnalysis_Autocad.dll.config and supplied it to the log4net for configuration. I think there is something else to it that I am not able to understand. I have started doubting whether we can log using log4net from autocad plugin? I also removed the environment path and hardcoded it so as to eliminate any issues regarding the reading of environment variable. Still no luck.

役に立ちましたか?

解決

On deploy, you should store the plugin configuration in one of the default config files:

  • User.config
  • acad.exe.config
  • machine.config

Probably, the User.config is the best file to carefully change during setup. Moreover you should design and code the changes so that they can be reverted on uninstall.

Doublecheck that you have properly replaced the ${NIRVANA_SOFTWARE_INSTALL_PATH} during setup and that the folder has proper permissions.

Furthermore, I would try to either add to the AssemblyInfo.cs the following line

[assembly: log4net.Config.XmlConfigurator(ConfigFile="AbsolutePathToTheConfig")]

(in which case you shouldn't call XmlConfigurator.Configure) or use an absolute path in the call to

log4net.Config.XmlConfigurator.Configure(new FileInfo("AbsolutePathToTheConfig"));

Finally, note that the log4net documentation about XmlConfigurator.Configure states that the configuration file used can be a XML not matching the .NET config rules.

The configuration file must be valid XML. It must contain at least one element called log4net that holds the log4net configuration data.

Thus, in the log4net_autocad.config file you can skip the <configuration> element:

<log4net>
    <appender name="AutocadRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Absolute\Path\To\Relay_Manager_Autocad.log"/>
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="0" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p [%C.%M] %m%n"/>
      </layout>
    </appender>
    <logger name="AutocadFile">
      <level value="All"/>
      <appender-ref ref="AutocadRollingFileAppender"/>
    </logger>
</log4net>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top