Message 1 of 12
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am adding `log4net` in my addin.
this is my config.
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss} %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
this is what I added in the .csproj
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.12" />
<None Include="Config\log4net.config">
<Link>Config\log4net.config</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<ItemGroup>
this is the code of the eternal command
using UI = Autodesk.Revit.UI;
using DB = Autodesk.Revit.DB;
using RA = Autodesk.Revit.Attributes;
using System.IO; // Path
using System.Reflection; // Assembly
using L4N = log4net;
using L4NC = log4net.Config;
namespace MyPlugin{
public class Logging {
public static L4N.ILog GetLogger() {
var dll_dir_info = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
if (!dll_dir_info.Exists) {
throw new Exception("dll directory does not exist");
}
string log_config_file = Path.Combine(dll_dir_info.FullName, "Config\\log4net.config");
if (!File.Exists(log_config_file)) {
throw new Exception("NLog.config does not exist (" + log_config_file + ")");
}
var config_stream = new FileInfo(log_config_file);
L4NC.XmlConfigurator.Configure(config_stream);
return L4N.LogManager.GetLogger(typeof(Logging));
}
}
[RA.Transaction(RA.TransactionMode.Manual)]
public class MyButton : UI.IExternalCommand {
public UI.Result Execute(UI.ExternalCommandData commandData, ref string message, DB.ElementSet elements) {
var logger = Logging.GetLogger();
logger.Info("angelo this");
logger.Debug("angelo that");
return UI.Result.Succeeded;
}
}
}
The execution of this code works on a simple C# executable, but when used by a Revit external command it does not print anything.
Same goes for an appender of type RollingFileAppender ....
Solved! Go to Solution.