[英]How to check Debug.Writeline() output in VS code?
我想知道在 VS Code 中使用 C# 擴展檢查 Debug.Writeline() 輸出的確切方法。
我使用 .net core 2.2 作為框架從這個網站https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault運行示例測試。鍵/樣本/Sample1_HelloWorld.cs 。
測試通過了,但我在終端中看不到 Debug.Writeline() 的任何輸出。 所以我用谷歌搜索,發現常見的解決方案是在我的 .cs 文件中包含以下代碼:
/* Create a listener that outputs to the console screen, and
add it to the debug listeners. */
TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);
但是,在我將代碼放入文件后,它在“Debug”類下找不到屬性“listeners”。 所以我再次用谷歌搜索並意識到這是因為這個屬性只包含在 .Net Framework 4.8 中。 因此,我下載了 .Net Framework v4.8 並將“TargetFramework”更改為如下:
<TargetFramework>net48</TargetFramework>
修改后,我再次運行測試:
dotnet test
但是結果還是這樣:
Microsoft (R) Test Execution Command Line Tool Version 16.2.0-preview-20190606-02
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
Test Run Successful.
Total tests: 1
Passed: 1
這是我的 .csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.0.0-preview.3"/>
<PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.0.0-preview.2"/>
<PackageReference Include="NUnit" Version="3.12.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="3.14.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0-preview-20190808-03"/>
</ItemGroup>
</Project>
這是我未經修改運行的示例測試:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
using Azure.Identity;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
namespace Azure.Security.KeyVault.Keys.Samples
{
/// <summary>
/// Sample demonstrates how to set, get, update and delete a key using the synchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
public partial class HelloWorld
{
[Test]
public void HelloWorldSync()
{
// Environment variable with the Key Vault endpoint.
string keyVaultUrl = Environment.GetEnvironmentVariable("AZURE_KEYVAULT_URL");
// Instantiate a key client that will be used to call the service. Notice that the client is using default Azure
// credentials. To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
// 'AZURE_CLIENT_KEY' and 'AZURE_TENANT_ID' are set with the service principal credentials.
var client = new KeyClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
// Let's create a RSA key valid for 1 year. If the key
// already exists in the Key Vault, then a new version of the key is created.
string rsaKeyName = $"CloudRsaKey-{Guid.NewGuid()}";
var rsaKey = new RsaKeyCreateOptions(rsaKeyName, hsm: false, keySize: 2048)
{
Expires = DateTimeOffset.Now.AddYears(1)
};
client.CreateRsaKey(rsaKey);
// Let's Get the Cloud RSA Key from the Key Vault.
Key cloudRsaKey = client.GetKey(rsaKeyName);
Debug.WriteLine($"Key is returned with name {cloudRsaKey.Name} and type {cloudRsaKey.KeyMaterial.KeyType}");
// After one year, the Cloud RSA Key is still required, we need to update the expiry time of the key.
// The update method can be used to update the expiry attribute of the key.
cloudRsaKey.Expires.Value.AddYears(1);
KeyBase updatedKey = client.UpdateKey(cloudRsaKey, cloudRsaKey.KeyMaterial.KeyOps);
Debug.WriteLine($"Key's updated expiry time is {updatedKey.Expires}");
// We need the Cloud RSA key with bigger key size, so you want to update the key in Key Vault to ensure
// it has the required size.
// Calling CreateRsaKey on an existing key creates a new version of the key in the Key Vault
// with the new specified size.
var newRsaKey = new RsaKeyCreateOptions(rsaKeyName, hsm: false, keySize: 4096)
{
Expires = DateTimeOffset.Now.AddYears(1)
};
client.CreateRsaKey(newRsaKey);
// The Cloud RSA Key is no longer needed, need to delete it from the Key Vault.
client.DeleteKey(rsaKeyName);
// To ensure key is deleted on server side.
Assert.IsTrue(WaitForDeletedKey(client, rsaKeyName));
// If the keyvault is soft-delete enabled, then for permanent deletion, deleted key needs to be purged.
client.PurgeDeletedKey(rsaKeyName);
}
private bool WaitForDeletedKey(KeyClient client, string keyName)
{
int maxIterations = 20;
for (int i = 0; i < maxIterations; i++)
{
try
{
client.GetDeletedKey(keyName);
return true;
}
catch
{
Thread.Sleep(5000);
}
}
return false;
}
}
}
這是修改后的測試:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
using Azure.Identity;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
namespace Azure.Security.KeyVault.Keys.Samples
{
/// <summary>
/// Sample demonstrates how to set, get, update and delete a key using the synchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
public partial class HelloWorld
{
[Test]
public static void Main()
{
/* Create a listener that outputs to the console screen, and
add it to the debug listeners. */
TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);
// Environment variable with the Key Vault endpoint.
string keyVaultUrl = Environment.GetEnvironmentVariable("AZURE_KEYVAULT_URL");
// Instantiate a key client that will be used to call the service. Notice that the client is using default Azure
// credentials. To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
// 'AZURE_CLIENT_KEY' and 'AZURE_TENANT_ID' are set with the service principal credentials.
var client = new KeyClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
// Let's create a RSA key valid for 1 year. If the key
// already exists in the Key Vault, then a new version of the key is created.
string rsaKeyName = $"CloudRsaKey-{Guid.NewGuid()}";
var rsaKey = new RsaKeyCreateOptions(rsaKeyName, hsm: false, keySize: 2048)
{
Expires = DateTimeOffset.Now.AddYears(1)
};
client.CreateRsaKey(rsaKey);
// Let's Get the Cloud RSA Key from the Key Vault.
Key cloudRsaKey = client.GetKey(rsaKeyName);
Debug.WriteLine($"Key is returned with name {cloudRsaKey.Name} and type {cloudRsaKey.KeyMaterial.KeyType}");
// After one year, the Cloud RSA Key is still required, we need to update the expiry time of the key.
// The update method can be used to update the expiry attribute of the key.
cloudRsaKey.Expires.Value.AddYears(1);
KeyBase updatedKey = client.UpdateKey(cloudRsaKey, cloudRsaKey.KeyMaterial.KeyOps);
Debug.WriteLine($"Key's updated expiry time is {updatedKey.Expires}");
// We need the Cloud RSA key with bigger key size, so you want to update the key in Key Vault to ensure
// it has the required size.
// Calling CreateRsaKey on an existing key creates a new version of the key in the Key Vault
// with the new specified size.
var newRsaKey = new RsaKeyCreateOptions(rsaKeyName, hsm: false, keySize: 4096)
{
Expires = DateTimeOffset.Now.AddYears(1)
};
client.CreateRsaKey(newRsaKey);
// The Cloud RSA Key is no longer needed, need to delete it from the Key Vault.
client.DeleteKey(rsaKeyName);
// To ensure key is deleted on server side.
// Assert.IsTrue(WaitForDeletedKey(client, rsaKeyName));
// If the keyvault is soft-delete enabled, then for permanent deletion, deleted key needs to be purged.
// client.PurgeDeletedKey(rsaKeyName);
}
private bool WaitForDeletedKey(KeyClient client, string keyName)
{
int maxIterations = 20;
for (int i = 0; i < maxIterations; i++)
{
try
{
client.GetDeletedKey(keyName);
return true;
}
catch
{
Thread.Sleep(5000);
}
}
return false;
}
}
}
最后,我嘗試在終端中鍵入以下內容:
dotnet run
我終於以這樣的格式從終端得到了我所期望的:
Key is returned with name CloudRsaKey-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and type xxx
Key's updated expiry time is x/xx/20xx x:xx:xx AM +00:00
但我想知道它是否是查看 Debug.Witeline() 輸出的唯一方法。 由於我沒有在終端中輸入dotnet test
命令,我認為我實際上並沒有在運行測試。 我很困惑。
首先,您必須設置一個環境變量,該變量將啟用托管在 dotnet 測試進程上的調試測試,以使用在 VS Code 中附加調試器的配置。 在帶有 Powershell 的 Windows 上,這是這樣完成的: $env:VSTEST_HOST_DEBUG=1
在沒有此設置的情況下運行dotnet test
只會通過。 如果不在調試模式下執行,甚至斷點也會被忽略。
一旦您將調試器附加到測試過程,您的斷點將被捕獲,您將能夠像往常一樣單步執行代碼,現在可以在 VS Code 的調試控制台中看到所有 Debug.WriteLine() 語句的輸出。
查看這篇文章以快速瀏覽。
這是一個簡短的演示:
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.