如何使用 C# 访问 Azure Webjobs 日志

How to access Azure Webjobs log using C#

我有一个 azure webjob 运行 定期运行,我需要访问 运行 日志和每个 运行.

的输出

我遇到了 kudu api,但无法继续,因为我不确定要通过我的网络请求传递哪些凭据。

示例代码:

private static void GetAPIData2()
    {
        String Url = "https://MyWebsite.scm.azurewebsites.net/azurejobs/api/jobs";
        WebRequest request = WebRequest.Create(Url);

        request.ContentType = "application/json";
        request.Method = "GET";

        //Your Azure FTP deployment credentials here
        request.Credentials = new NetworkCredential("What should go here?", "and here?");

        WebResponse response = request.GetResponse();
        Console.WriteLine(response.ToString());
    }

如果使用我的 azure 管理员帐户,我会收到 401 Unauthorized。

我应该使用什么用户名和密码进行身份验证?

请查看 this page 以了解您可以传递给这些 API 的凭据类型。

您还可以使用自定义 TraceWriter。此处示例:https://gist.github.com/aaronhoffman/3e319cf519eb8bf76c8f3e4fa6f1b4ae

它与答案没有直接关系,但我还发现在 PowerShell 中可以使用从 Get-AzAccessToken cmdlet 获取的 Bearer 令牌调用 WebJobs API:

$token = Get-AzAccessToken
$token.Token

(尚未找到在 C# 中获取它的方法,但可能可以通过检查 Get-AzAccessToken 的代码找到它)