question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disable log in custom AuthenticationHandler

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

No response

Describe the solution you’d like

I can’t disable log by using "Logging": { "LogLevel": { "Microsoft.AspNetCore.Authentication": "Warning" } } in appsettings.json, but if I set logging level for a custom AuthenticationHandler. It will disable all other logs that I provided. I just want to disable the logs from “Microsoft.AspNetCore.Authentication”, the parent class.

Using “Microsoft.AspNetCore.Authentication”: “Warning” to change log level only for the parent class.

Additional context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Marusykcommented, Apr 10, 2022

You’d need to give your logger in the derived class a different category name so you can configure it independently from the base class.

@Tratcher Could you please explain how it will work because the logs from the Base and Derived will have the same logger category name? Look at the code:

public class MyAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
	private readonly ILogger<MyAuthenticationHandler> _logger;

	public MyAuthenticationHandler(
		IOptionsMonitor<AuthenticationSchemeOptions> options,
		ILoggerFactory logger,
		UrlEncoder encoder,
		ISystemClock clock)
		: base(options, logger, encoder, clock)
	{
		_logger = logger.CreateLogger<MyAuthenticationHandler>();
	}
	
	protected override Task<AuthenticateResult> HandleAuthenticateAsync()
	{
		_logger.LogWarning("MyAuthenticationHandler");	
		return Task.FromResult(AuthenticateResult.Success(GetAuthenticationTicket()));
	}
}

The logs:

MyAuthenticationHandler <-- logger category == MyAuthenticationHandler AuthenticationScheme: MyAuthenticationScheme was challenged. <-- logger category == MyAuthenticationHandler

We need to know how to disable logs from the AuthenticationHandler class (AuthenticationScheme: MyAuthenticationScheme was challenged.).

Should we then create something like this in MyAuthenticationHandler :

_logger = logger.CreateLogger("DummyMyAuthenticationHandler");

The logs:

MyAuthenticationHandler <-- logger category == DummyMyAuthenticationHandler AuthenticationScheme: MyAuthenticationScheme was challenged. <-- logger category == MyAuthenticationHandler

then filter all logs for DummyMyAuthenticationHandler to hide “AuthenticationScheme: MyAuthenticationScheme was challenged.”?

{
  "Logging": {
    "LogLevel": {
      "MyAuthenticationHandler": "Critical" 
  }
  ...
}

after this the logs:

MyAuthenticationHandler <-- logger category == DummyMyAuthenticationHandler

It looks strange!

It would be better to change this https://github.com/dotnet/aspnetcore/blob/34f71bb22ce65e59c9b9bfbe3e183db395ef5595/src/Security/Authentication/Core/src/AuthenticationHandler.cs#L112 to Logger = logger.CreateLogger<AuthenticationHandler>();

Then the log will be:

MyAuthenticationHandler <-- logger category == MyAuthenticationHandler AuthenticationScheme: MyAuthenticationScheme was challenged. <-- logger category == AuthenticationHandler

So, just add

{
  "Logging": {
    "LogLevel": {
      "Microsoft.AspNetCore.Authentication": "Warning" 
  }
  ...
}

after this the logs will be expected:

MyAuthenticationHandler <-- logger category == MyAuthenticationHandler

1reaction
Tratchercommented, Mar 29, 2022

You’d need to give your logger in the derived class a different category name so you can configure it independently from the base class.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to turn off the logging done by the ASP.NET core ...
Since your logger provider is a custom one, you will have to configure it yourself. Take a look on how the console logger...
Read more >
Working with custom authentication schemes in ASP.NET ...
The authentication handler in its most basic form only implements the HandleAuthenticateAsync method, by overriding it.
Read more >
Overview of ASP.NET Core Authentication
To disable automatically using the single authentication scheme as the DefaultScheme , call AppContext.SetSwitch("Microsoft.AspNetCore.
Read more >
Authorize with a specific scheme in ASP.NET Core
This article explains how to limit identity to a specific scheme when working with multiple authentication methods.
Read more >
Authentication Handler in AEM: custom approach
If we want to avoid that we can disable the anonymous access to it via Apache Sling Authentication Service. We can also filter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found