runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Debug logging provider is not writing to /var/log/syslog on Ubuntu 20.04

Open explorest opened this issue 5 years ago • 7 comments

Description

The scenario is simplest possible demo console application using hosting/DI with one IHostedService . Everything on default settings. The service itself is also the simplest possible IHostedService implementation and all it is doing is taking an ILogger<TCategory> through construction injection and logging messages in its StartAsync and StopAsync methods through _logger.LogInformation() calls. Since the documentation here =>https://docs.microsoft.com/en-us/dotnet/core/extensions/logging-providers#debug mentions that on a linux system the debug provider writes to either /var/log/message or /var/log/syslog and my system has /var/log/syslog --- I looked for logger's output in /var/log/syslog but it doesn't go there. The logger's output to console is perfectly normal. Here are a couple of things I tried:

  1. Toggled the environment between Production and Development but it didn't work.
  2. System.Diagnostics.Debug.WriteLine("log message"); did work ! . It wrote the message to /var/log/syslog.
  3. dotnet publish followed by copying the publish folder to another folder and running the application from there did not work.
  4. When using VSCode to manage the project: the logger.LogInformation() calls do write debug provider output to VSCode's Debug Console after I let VSCode to auto-create "launch.json" and "tasks.json" in its .vscode directory in projects root and run the project with F5 rather than dotnet run on command line!

So the only thing that writes to the /var/log/syslog is directly using System.Diagnostics.Debug.WriteLine(). The ILogger<Tcategory> that is injected into my service via DI does NOT post to /var/log/syslog under any circumstance -- the only place I managed to make it post its output to is VSCode's Debug console.


PROJECT and ENVIRONMENT DETAILS:

1. There are only 2 files in the project root :
-- Program.cs -- .csproj file.
Nothing else. No appsettings.

2. Program.cs file (containing the entire application including the IHostedService implementation) :

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

namespace t1
{
    class Program
    {
        static Task Main(string[] args)
        {
            using IHost host= Host.CreateDefaultBuilder(args).ConfigureServices( 
                    (hostBuilderContext , serviceCollection) =>
                {      
                      serviceCollection.AddHostedService<TestService>();
                }

            ).UseEnvironment(Microsoft.Extensions.Hosting.Environments.Development)
            
            .Build();

            return host.RunAsync();          
        }
    }
    
    
    

   class TestService: IHostedService
   {
        ILogger<TestService> _logger;

        public TestService(ILogger<TestService> logger)
        {


            _logger=logger; 


        }
    
       public Task StartAsync (CancellationToken cancellationToken)
       {
            _logger.LogInformation("1. StartAsync called ..." );

            return Task.CompletedTask;

       }

      public Task StopAsync (CancellationToken cancellationToken)
      {

            _logger.LogInformation("2. StopAsync called.");

          

            return Task.CompletedTask;


        }
    
    }
}


3. csproj project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
  </ItemGroup>

</Project>



Configuration

$ dotnet --info

.NET SDK (reflecting any global.json):
 Version:   5.0.300
 Commit:    2e0c8c940e

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/5.0.300/

Host (useful for support):
  Version: 5.0.6
  Commit:  478b2f8c0e

.NET SDKs installed:
  3.1.409 [/usr/share/dotnet/sdk]
  5.0.300 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.15 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Regression?

No regressions

Other information

The only work around that's working is a direct call to System.Diagnostics.Debug.WriteLine(). This call does write to the /var/log/syslog. I could not find any other workaround.

explorest avatar May 29 '21 05:05 explorest

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar May 29 '21 05:05 ghost

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

area-Extensions-Logging os-alpine

explorest avatar May 29 '21 09:05 explorest

Tagging subscribers to this area: @maryamariyan See info in area-owners.md if you want to be subscribed.

Issue Details

Description

The scenario is simplest possible demo console application using hosting/DI with one IHostedService . Everything on default settings. The service itself is also the simplest possible IHostedService implementation and all it is doing is taking an ILogger<TCategory> through construction injection and logging messages in its StartAsync and StopAsync methods through _logger.LogInformation() calls. Since the documentation here =>https://docs.microsoft.com/en-us/dotnet/core/extensions/logging-providers#debug mentions that on a linux system the debug provider writes to either /var/log/message or /var/log/syslog and my system has /var/log/syslog --- I looked for logger's output in /var/log/syslog but it doesn't go there. The logger's output to console is perfectly normal. Here are a couple of things I tried:

  1. Toggled the environment between Production and Development but it didn't work.
  2. System.Diagnostics.Debug.WriteLine("log message"); did work ! . It wrote the message to /var/log/syslog.
  3. dotnet publish followed by copying the publish folder to another folder and running the application from there did not work.
  4. When using VSCode to manage the project: the logger.LogInformation() calls do write debug provider output to VSCode's Debug Console after I let VSCode to auto-create "launch.json" and "tasks.json" in its .vscode directory in projects root and run the project with F5 rather than dotnet run on command line!

So the only thing that writes to the /var/log/syslog is directly using System.Diagnostics.Debug.WriteLine(). The ILogger<Tcategory> that is injected into my service via DI does NOT post to /var/log/syslog under any circumstance -- the only place I managed to make it post its output to is VSCode's Debug console.


PROJECT and ENVIRONMENT DETAILS:

1. There are only 2 files in the project root :
-- Program.cs -- .csproj file.
Nothing else. No appsettings.

2. Program.cs file (containing the entire application including the IHostedService implementation) :

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

namespace t1
{
    class Program
    {
        static Task Main(string[] args)
        {
            using IHost host= Host.CreateDefaultBuilder(args).ConfigureServices( 
                    (hostBuilderContext , serviceCollection) =>
                {      
                      serviceCollection.AddHostedService<TestService>();
                }

            ).UseEnvironment(Microsoft.Extensions.Hosting.Environments.Development)
            
            .Build();

            return host.RunAsync();          
        }
    }
    
    
    

   class TestService: IHostedService
   {
        ILogger<TestService> _logger;

        public TestService(ILogger<TestService> logger)
        {


            _logger=logger; 


        }
    
       public Task StartAsync (CancellationToken cancellationToken)
       {
            _logger.LogInformation("1. StartAsync called ..." );

            return Task.CompletedTask;

       }

      public Task StopAsync (CancellationToken cancellationToken)
      {

            _logger.LogInformation("2. StopAsync called.");

          

            return Task.CompletedTask;


        }
    
    }
}


3. csproj project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
  </ItemGroup>

</Project>



Configuration

$ dotnet --info

.NET SDK (reflecting any global.json):
 Version:   5.0.300
 Commit:    2e0c8c940e

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/5.0.300/

Host (useful for support):
  Version: 5.0.6
  Commit:  478b2f8c0e

.NET SDKs installed:
  3.1.409 [/usr/share/dotnet/sdk]
  5.0.300 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.15 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Regression?

No regressions

Other information

The only work around that's working is a direct call to System.Diagnostics.Debug.WriteLine(). This call does write to the /var/log/syslog. I could not find any other workaround.

Author: explorest
Assignees: -
Labels:

area-Extensions-Logging, area-System.Diagnostics-coreclr, untriaged

Milestone: -

ghost avatar Jun 14 '21 16:06 ghost

@explorest you have labeled this issue with os-alpine . Does this issue happen on Ubuntu and Alpine too?

tarekgh avatar Aug 11 '22 20:08 tarekgh

Hi @explorest,

By design the DebugLogger does not act unless the debugger is attached. This would prevent the call that would write to /var/log/syslog or /var/log/message on Linux distributions because the message will either write to the debugger or not at all. The doc page you referenced is inaccurate and needs to be updated. A PR has been created to resolve this.

The workaround for this is to create a custom logging provider that directly calls System.Diagnostics.Debug.WriteLine() without checking if the debugger is attached.

Nick-Stanton avatar Aug 11 '22 22:08 Nick-Stanton

Thanks @Nick-Stanton for opening the PR to fix the doc.

@explorest

Just to mention, the DebugLogger doc clearly states A logger that writes messages in the debug output window only when a debugger is attached..

By that we are closing this issue. Feel free to send any questions if you still have any. Thanks for your report.

tarekgh avatar Aug 11 '22 22:08 tarekgh

Thanks a lot @Nick-Stanton and @tarekgh for resolving this. I remember this issue confusing the hell out of me. I even went to the dotnet core repo and ploughed through the source code of the Debug provider and related types to figure out what exactly is happening. That confused me even more -- lol. Thanks again .
Regards.

explorest avatar Aug 12 '22 14:08 explorest