通过 Azure KeyVault 抑制控制台日志记录

Suppressing Console Logging by Azure KeyVault

我正在 Net Core 2 控制台应用程序中使用 C# 访问 Azure 密钥保管库。每当应用程序运行时,控制台 window 都会在我从保险库中检索秘密时收到类似这样的消息:

2017-12-26T18:03:49.8610049Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - AcquireTokenHandlerBase.cs: ADAL PCL.CoreCLR with assembly version '3.17.3.35304', file version '3.17.41219.2324' and informational version 'b6afaeae7cff965e66649e0ee7e8c29071d5a7e6' is running... 2017-12-26T18:03:49.8621855Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - AcquireTokenHandlerBase.cs: === Token Acquisition started: Authority: https://login.windows.net/[...]/ Resource: https://vault.azure.net ClientId: [...] CacheType: null Authentication Target: Client

2017-12-26T18:03:49.8981211Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - AcquireTokenHandlerBase.cs: Loading from cache.

2017-12-26T18:03:49.9010018Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - TokenCache.cs: Looking up cache for a token...

2017-12-26T18:03:49.9060990Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - TokenCache.cs: No matching token was found in the cache

2017-12-26T18:03:50.2185881Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - TokenCache.cs: Storing token in the cache...

2017-12-26T18:03:50.2211369Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - TokenCache.cs: An item was stored in the cache

2017-12-26T18:03:50.2351165Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - AcquireTokenHandlerBase.cs: === Token Acquisition finished successfully. An access token was retuned:

    Access Token Hash: [...]
    Expiration Time: 12/26/2017 7:03:49 PM +00:00
    User Hash: null

我不记得在我的代码中做过任何事情来为密钥保管库访问设置任何类型的日志记录。但是,我已经在整个应用程序中配置了 Serilog 的 ILogger,所以也许它正在以某种方式被拾取。

如何禁止显示这些消息?

根据你的描述,我查看了Microsoft.Azure.KeyVault的源代码,但是没有找到相关的日志记录。

2017-12-26T18:03:49.8610049Z: 29c98a86-9e1d-4a5d-86d6-daf8f2cfdc56 - AcquireTokenHandlerBase.cs: ADAL PCL.CoreCLR with assembly version '3.17.3.35304', file version '3.17.41219.2324' and informational version

根据日志信息,尝试利用ILSpy to decompile the package Microsoft.IdentityModel.Clients.ActiveDirectory,发现如下代码:

您可以通过以下代码禁用 ADAL 库下的跟踪日志记录:

LoggerCallbackHandler.UseDefaultLogging = false;

测试:

对于 Microsoft.IdentityModel.Clients.ActiveDirectory2.x 版本,您需要这样调用:

using Microsoft.IdentityModel.Clients.ActiveDirectory;

AdalTrace.LegacyTraceSwitch.Level = TraceLevel.Error;

对于版本 3.x+ 使用 Bruce_Chen 的答案:

LoggerCallbackHandler.UseDefaultLogging = false;