Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Nlog in an add-in project for Revit

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Anonymous
1626 Views, 3 Replies

Nlog in an add-in project for Revit

Hello, 

I have an issue with using a logger such as nlog in a C# project that I use as an add-in in revit.
I can't create a log file with this logger, or even display the log in the console.

I try my config in another simple console project and it's work fine and when try it with the revit add-in it doesn't. 

there is my nlog config file:

 

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      internalLogLevel="Info" internalLogFile="c:\Temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>

    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->


    <!--Write  events to a file with the date in the filename.-->
    <target xsi:type="File" name="file" fileName="${basedir}/logs/log-${shortdate}.log"
            layout="${uppercase:${level}} ${longdate}  : ${message}"
            archiveAboveSize="10240000" />
    <target name="logconsole" xsi:type="Console" 
layout=" ${uppercase:${level}} ${longdate}  : ${message}"/>
  </targets>

  <rules>
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"-->
    <logger name="*" minlevel="Debug" writeTo="file" />
    <logger name="*" minlevel="Debug" writeTo="logconsole" />
  </rules>
</nlog>

log instance in each class i want to log 

 [Transaction(TransactionMode.Manual)]
    public class Base : IExternalCommand
    {   
        private static Logger logger = LogManager.GetCurrentClassLogger();

Do I need to do anything else?

Thanks for any help!

 

Tags (1)
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: Anonymous

I finally solved my problem, I put the solution in case another person needs it.

nlog didn't load the config file when launched with REVIT. so I add the file path with the method

LogManager.Configuration = new XmlLoggingConfiguration(YOURCONFIGFILEPATH);

 

Message 3 of 4
Extraneous
in reply to: Anonymous

@Anonymous thank you, I have the same problem - nlog don't create folder and log files. I noticed LogManager.Configuration was Null and try you method. But it not helps me, log files dont creates whatever. Do you know what could be the problem?

Extraneous_0-1593715286426.png

My config file:

 

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <targets>
    <target xsi:type="File" name="filedata" fileName="${basedir}/logs/${shortdate}.log"
                      layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="filedata" />
  </rules>
</nlog>

 

And addin-code:

 

class CommandBatchPrint : IExternalCommand
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {            
            App.assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

            string nlogconfigpath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(App.assemblyPath), "NLog.config");

            LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(nlogconfigpath);
            logger.Debug("Print started");

 

 

Alexander Zuev
In BIM we trust
Facebook | Linkedin | Telegram

Message 4 of 4

The following works for me:

string nlogConfigPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "NLog.config");
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(nlogConfigPath);
Logger logger = LogManager.GetCurrentClassLogger();
logger.Info("Info: The Add-in is starting");

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report