如何使用azure webjobs SDK添加有关异常处理的自定义数据?

问题描述 投票:0回答:1

我有一个简单的Azure函数,它返回到队列:

    private readonly TelemetryClient _telemetryClient;

    [return: Queue("%ReturnQueue%")]
    public async Task<string> Run([QueueTrigger("%RequestQueue%")] string msg, ILogger log)
    {
        try
        {
            //Some dependency calls   
        }
        catch(Exception ex)
        {
            var dic = new Dictionary<string,string>();
            dic.Add("Id", someId);
            dic.Add("CustomData", cusomData);

            _telemetryClient.TrackException(ex, dic);
        }
    }

我显然收到一个编译错误,指出并非所有代码路径都返回一个值。问题是,如果我在catch块的末尾添加了throw,则Azure Functions运行时会在appinsights门户上复制该专有内容。如何将自定义数据添加到这样的异常中?

azure azure-functions azure-webjobs
1个回答
0
投票
public class MyCustomException : Exception { public string Id {get;set;} public string CustomData {get;set;} public Exception RootException {get;set;} public MyCustomException(string id, string customData, Exception ex) { Id = id; CustomData = customData; RootException = ex; } } private readonly TelemetryClient _telemetryClient; [return: Queue("%ReturnQueue%")] public async Task<string> Run([QueueTrigger("%RequestQueue%")] string msg, ILogger log) { try { //Some dependency calls } catch(Exception ex) { //var dic = new Dictionary<string,string>(); //dic.Add("Id", someId); //dic.Add("CustomData", cusomData); var customEx = new MyCustomException(someId, cusomData, ex); _telemetryClient.TrackException(customEx); } }

PS:在MyCustomException中,您实际上可以具有Dictionary而不是字符串属性。

© www.soinside.com 2019 - 2024. All rights reserved.