Nlog: ColoredConsole target not colored in Visual Studio 2019/Asp.Net Core 3

Created on 15 Nov 2019  路  8Comments  路  Source: NLog/NLog

While settings up Nlog for a new asp.net core 3 project, i tried to use the ColoredConsole target but couldn't get any coloring working in Visual Studio Community 2019.

Has anyone ever got it working ?

NLog version: 4.6.8
NLog.Web.AspNetCore version: 4.9.0

Platform: Asp.NET Core 3 on Windows 10, using Visual studio 2019 Community edition and IISExpress as a webserver

Current NLog config (xml or C#, if relevant)

<?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"
      autoReload="true"
      throwExceptions="true"
      internalLogLevel="Info"
      internalLogFile="c:\temp\internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <target xsi:type="ColoredConsole"
          name="Console"
          layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
          useDefaultRowHighlightingRules="true"
          enableAnsiOutput="false"
          DetectOutputRedirected="false"/>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*" minlevel="Trace" writeTo="Console" />
  </rules>
</nlog>

  • What is the current result?
    In the Output > [Name of the project] - ASP.Net Core Web Server window, the ColoredConsole target should write logs doesn't use coloring.
    image

  • What is the expected result?
    In the Output > [Name of the project] - ASP.Net Core Web Server window, the ColoredConsole target should write logs with colors.

  • Please post full exception details (message, stacktrace, inner exceptions)
    My log code in a WebAPI controller :
    image

  • Are there any workarounds? YES
    This bug occurs only in the Output window of visual studio.
    By Default on windows, IISExpress is selected as the default engine for running asp.net core project :
    image
    If you change target and switch it the the project name (i.e
    image), a new terminal window is spawned and the program output is redirected to that terminal.
    Result is
    image and coloring is applied.

Has anyone got it working in the Output window of Visual Studio ?

console-target external question

All 8 comments

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

Think the Visual Studio "Output Window" is not a real console window. It just displays the console output from the application.

Try changing the NLog console-Layout to begin with (Putting ${level} first, and remember the colon):

  • "Error: " - for errors
  • "Warning: " - for warnings:

See also: https://stackoverflow.com/a/54004318/193178

To extend the pattern-matching even further, then you can try VSColorOutput: https://marketplace.visualstudio.com/items?itemName=MikeWard-AnnArbor.VSColorOutput

@snakefoot maybe we could add an option to prefix those levels,

e.g.

visualStudioOutputColors=true.

or do you think that's to specific? Doubt here.

Think it should just be part of the Wiki for console-target. Think people will be confused by Layout being changed.

Added, feel free to edit:

https://github.com/NLog/NLog/wiki/ColoredConsole-target#visual-studio-output-window

@Land3r Have your question been answered?

Please add the requested info, so we could help you better! (This issue will be closed in 7 days)

Yes, i also think that this didn't require any additional source code, an appendix in the wiki is all that is required :)

I ended up using a regular console with the VsColorOutpu Visual studio extension.

Thanks a lot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rapiiidooo picture Rapiiidooo  路  3Comments

FaMouZx3 picture FaMouZx3  路  3Comments

sszost picture sszost  路  3Comments

JustArchi picture JustArchi  路  3Comments

haythamabutair picture haythamabutair  路  3Comments