69

I'm trying to send the output to the console (or colouredconsole) ... which I'm hoping would (also?) go to the Visual Studio's Output window for any ASP.NET web site/app/mvc app.

It doesn't by default, but if I change the target to 'file' then it works for sure.

Can NLog output to the Output window for web apps?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

2 Answers2

149

You can use this configuration file (nlog.config in the app path):

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

  <targets>
        <target name="debugger" xsi:type="Debugger" layout="${logger}::${message}"/>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="debugger" />
  </rules>
</nlog>

See also: https://github.com/NLog/NLog/wiki/Debugger-target

-Scott

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Scott P
  • 3,775
  • 1
  • 24
  • 30
  • 6
    AWESOMESAUCE! i never knew there was a type == Debugger. WINNAH! – Pure.Krome Nov 05 '08 at 00:29
  • 1
    It works fine with debugging. Although is there anyway to make it work without debugging as well? – liang Jul 03 '14 at 14:42
  • 8
    It's worth noting that this comes at a severe performance penalty. Writing a lot of output to the Debugger target made my applications extremely sluggish. Don't enable this in production! Surprisingly, writing to a File target seems to be much faster, even though File means disk I/O and Debugger doesn't. I'm not sure if this is the fault of NLog or Visual Studio. – chris Aug 23 '15 at 10:22
  • 1
    Thanks! I feel like they should include the words 'Visual Studio output window' in the help. Here's the NLog help page about the Debugger target: https://github.com/NLog/NLog/wiki/Debugger-target. I configured all of my files to also write to the debugger by adding debugger to the write to: `writeTo="fileLogger,debugger"` :) Now. How do I make it appear in a different 'Show output from' in the dropdown? I'll go ask that separately... – Ian Grainger Feb 11 '16 at 11:23
1

Adding to Scott P's answer, you can add a filter for when the environment is not "Development" to prevent any slowdowns in Staging/Production etc.

<logger name="*" minlevel="Trace" writeTo="debugger">
    <filters defaultAction="Ignore">
        <when condition="'${environment:ASPNETCORE_ENVIRONMENT}' == 'Development'" action="Log" />
    </filters>
</logger>
NickG
  • 9,315
  • 16
  • 75
  • 115