>I wanted to log the execution of my add-in, and so I've chosen log4net.
In what programming language have you written the add-in ?
Below is an example using Visual Studio 2012 Express Edition and C#
1. Create a <<yourAddin>> Class library DLL. Choose of course your own suitable name for <<yourAddin>>
2. Set the checkbox [ x ] Register for COM Interop in the Project Settings -> Build tab
3. Build the project.
If you run with Administrative rights, Visual Studio will also register the DLL with regasm.exe.
Or if you prefer:
Register manually <<yourAddin>>.DLL with a command below similar to this:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe D:\Temp\<<yourAddin>>.dll /codebase
4. Copy the log4net.config configuration file near the D:\Temp\<<yourAddin>>.dll
Or if you prefer, copy the log4net.config file in the Sparx Enterprise Architect folder
"C:\Program Files (x86)\Sparx Systems\EA\log4net.config"
But this requires Administrative rights and in this case is not necessary to call log4net.Config.XmlConfigurator.ConfigureAndWatch
5. In AssemblyInfo.cs, add the line below to the end of the file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Or if you prefer, set a full path to the configuration file, in which case, again, is not necessary to call log4net.Config.XmlConfigurator.ConfigureAndWatch
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "D:\\Temp\\log4net.config", Watch = true)]
6. Write the code below in the main class of the DLL
using EA;
using log4net;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
[ComVisible(true)]
public class AddIn
{
private ILog log;
public string EA_Connect(Repository repository)
{
//setup the configuration file
//in this example, at step 4, I decide to use the
//log4net.config configuration file placed near the <<yourAddin>>.DLL file
//this is is not necessary if the log4net.config is in the Sparx Enterprise Architect folder or a full path is specified in AssemblyInfo.cs
string dllFullPath = Assembly.GetExecutingAssembly().Location;
String logFile = Path.GetDirectoryName(dllFullPath)+"\\log4net.config";
//https://stackoverflow.com/questions/21166126/log4net-separate-config-file-not-working
FileInfo fileInfo = new FileInfo(logFile);
log4net.Config.XmlConfigurator.ConfigureAndWatch(fileInfo);
log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Info("\r\nEA_Connect '" + repository.ConnectionString + "'");
return "AddIn connected";
}
….
}
Example of log4net.config file
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="MyAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<appender name="MyFileAppender" type="log4net.Appender.FileAppender">
<file value="D:\Temp\MainLog.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd-MM-yyyy HH:mm:ss} %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="MyAppender" />
<appender-ref ref="MyFileAppender" />
</root>
</log4net>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>