You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure内容审核API错误处理:如何将异常消息存入审计日志变量

解决方案

你需要在调用Azure Content Moderation API的代码外层包裹try-catch块,捕获API返回的异常并提取消息,之后将消息传入审计日志的ExceptionMessage字段。

具体实现逻辑:

  • 捕获Azure API调用时的各类异常(比如HttpRequestException、Azure SDK专属异常),提取完整异常信息(包含内部异常内容,避免遗漏故障细节)。
  • 将异常消息存入变量,在创建ProfanityAuditLog时赋值给对应字段。
  • 无论触发脏话拒绝还是警告逻辑,只要API出现异常,都要把异常信息记录到审计日志中;若API异常但未触发业务规则,也建议单独记录日志。

修改后的代码示例:

string azureExceptionMessage = null;

try
{
    // 这里放置调用Azure Content Moderation API的代码
    // 示例:var screenResult = await _contentModerationClient.ScreenTextAsync(...)
}
catch (Exception ex)
{
    // 提取完整异常信息(含堆栈),也可按需只取消息:ex.Message + (ex.InnerException != null ? $" | {ex.InnerException.Message}" : "")
    azureExceptionMessage = ex.ToString();
}

// 业务判断逻辑
if (isProfanity)
{
    var log = new ProfanityAuditLog
    {
        Username = text,
        DetectedLanguage = detectedLanguage?.DetectedLanguageProperty,
        IsProfanity = isProfanity,
        ThresholdCategory1 = _options.ThresholdCategory1,
        ThresholdCategory2 = _options.ThresholdCategory2,
        ThresholdCategory3 = _options.ThresholdCategory3,
        ExceptionMessage = azureExceptionMessage
    };
    await _profanityAuditRepo.AddLogAsync(log);
    
    throw new RemoteException("The username contains profanity. Please pick a different one.",
        "The username contains profanity and has been denied.",
        RemoteExceptionType.ProfileUsernameProfanityException);
}
else if (warning)
{
    var log = new ProfanityAuditLog
    {
        Username = text,
        DetectedLanguage = detectedLanguage?.DetectedLanguageProperty,
        IsProfanity = isProfanity,
        ThresholdCategory1 = _options.ThresholdCategory1,
        ThresholdCategory2 = _options.ThresholdCategory2,
        ThresholdCategory3 = _options.ThresholdCategory3,
        ExceptionMessage = azureExceptionMessage
    };
    await _profanityAuditRepo.AddLogAsync(log);
    
    throw new RemoteException("The username might contain profanity.",
        "The username might contain profanity and will have to be validated by support.",
        RemoteExceptionType.ProfileUsernameProfanityWarningException);
}
// 处理API异常但未触发业务规则的情况
else if (!string.IsNullOrEmpty(azureExceptionMessage))
{
    var log = new ProfanityAuditLog
    {
        Username = text,
        DetectedLanguage = detectedLanguage?.DetectedLanguageProperty,
        IsProfanity = isProfanity,
        ThresholdCategory1 = _options.ThresholdCategory1,
        ThresholdCategory2 = _options.ThresholdCategory2,
        ThresholdCategory3 = _options.ThresholdCategory3,
        ExceptionMessage = azureExceptionMessage
    };
    await _profanityAuditRepo.AddLogAsync(log);
}

补充说明:

  • 使用ex.ToString()可以保留完整的异常堆栈,方便后续排查API故障;如果只需要简洁的错误描述,可改用ex.Message并拼接内部异常的消息。
  • 异步方法需用await调用AddLogAsync,避免日志未完成写入就抛出异常。

内容的提问来源于stack exchange,提问作者Spottie97

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 22:24:54