C# 未通过遥测客户端在ApplicationInsights上发布的跟踪和异常

C# 未通过遥测客户端在ApplicationInsights上发布的跟踪和异常,c#,azure,azure-functions,azure-application-insights,C#,Azure,Azure Functions,Azure Application Insights,情况就是这样。 我们有一个由服务总线触发的Azure函数,它应该收集消息并将它们发布到ApplicationInsights。 这是我们为函数编写的代码 public class TopicToApplicationInsightsLogs { private readonly IApplicationInsightsService _appInsightsService; public TopicToApplicationInsightsLogs(IApplication

情况就是这样。

我们有一个由服务总线触发的Azure函数,它应该收集消息并将它们发布到ApplicationInsights。

这是我们为函数编写的代码

public class TopicToApplicationInsightsLogs
{
    private readonly IApplicationInsightsService _appInsightsService;

    public TopicToApplicationInsightsLogs(IApplicationInsightsService appInsightsService)
    {
        _appInsightsService = appInsightsService;
    }

    [FunctionName("TopicToApplicationInsightsLogs")]
    public async Task Run(
        [ServiceBusTrigger("%MonitorLogTopic%", "%MonitorLogSubcription%", Connection = "MonitorLogServiceBusConnectionString")]
        string jsonData,
        ILogger log,
        MessageReceiver messageReceiver,
        string lockToken
    )
    {
        var jObject = JObject.Parse(jsonData);
        var data = jObject.GetValue("Data").ToObject<Common.Domain.Payloads.Entities.MonitorLog>();

        try
        {
            foreach (var edgeLog in data.Data.Logs)
            {
                _appInsightsService.TrackTrace(edgeLog);

                if (!string.IsNullOrWhiteSpace(edgeLog.SerializedException))
                {
                    _appInsightsService.TrackException(edgeLog);
                }

                _appInsightsService.Flush();
            }

            await messageReceiver.CompleteAsync(lockToken);
            log.LogInformation($"Posted {data.Data.Logs.Count()} posts.");
        }
        catch (Exception ex)
        {
            await messageReceiver.DeadLetterAsync(lockToken);
            log.LogError($"Error posting logs: {ex.Message}", ex);

            throw;
        }
    }
}
以及用于映射对象的
应用程序insightsparser.cs

public static class ApplicationInsightsParser
{
    public static TraceTelemetry ToTraceTelemetry(this MonitorLogDataRecord log)
    {
        return new TraceTelemetry
        {
            Timestamp = log.Timestamp,
            //Properties = {{"", ""}},
            Context =
            {
                //Component =
                //{
                //    Version = ""
                //},
                //Device =
                //{
                //    Id = "",
                //    Model = "",
                //    OemName = "",
                //    OperatingSystem = "",
                //    Type = ""
                //},
                //Cloud =
                //{
                //    RoleInstance = "",
                //    RoleName = ""
                //},
                //Flags = 0,
                //InstrumentationKey = "",
                //Location =
                //{
                //    Ip = ""
                //},
                Operation =
                {
                    Name = log.Source
                    //CorrelationVector = "",
                    //Id = "",
                    //ParentId = "",
                    //SyntheticSource = ""
                }
                //Session =
                //{
                //    Id = "",
                //    IsFirst = true
                //},
                //User =
                //{
                //    Id = "",
                //    AccountId = "",
                //    AuthenticatedUserId = "",
                //    UserAgent = ""
                //},
                //GlobalProperties = {{"", ""}}
            },
            //Extension = null,
            //Sequence = "",
            //ProactiveSamplingDecision =SamplingDecision.None,
            Message = log.Content,
            SeverityLevel = log.Level.ParseToSeverity()
        };
    }

    public static ExceptionTelemetry ToExceptionTelemetry(this MonitorLogDataRecord log)
    {
        return new ExceptionTelemetry
        {
            Timestamp = log.Timestamp,
            //Properties = {{"", ""}},
            Context =
            {
                //Component =
                //{
                //    Version = ""
                //},
                //Device =
                //{
                //    Id = "",
                //    Model = "",
                //    OemName = "",
                //    OperatingSystem = "",
                //    Type = ""
                //},
                //Cloud =
                //{
                //    RoleInstance = "",
                //    RoleName = ""
                //},
                //Flags = 0,
                //InstrumentationKey = "",
                //Location =
                //{
                //    Ip = ""
                //},
                Operation =
                {
                    Name = log.Source
                    //CorrelationVector = "",
                    //Id = "",
                    //ParentId = "",
                    //SyntheticSource = ""
                }
                //Session =
                //{
                //    Id = "",
                //    IsFirst = true
                //},
                //User =
                //{
                //    Id = "",
                //    AccountId = "",
                //    AuthenticatedUserId = "",
                //    UserAgent = ""
                //},
                //GlobalProperties =
                //{
                //    {"", ""}

                //}
            },
            //Extension = null,
            //Sequence = "",
            //ProactiveSamplingDecision = SamplingDecision.None,
            //Message = log.Content,
            SeverityLevel = log.Level.ParseToSeverity(),
            //Metrics =
            //{
            //    {"", 0}
            //},
            Exception = JsonConvert.DeserializeObject<Exception>(log.SerializedException)
            //ProblemId = ""
        };
    }

    private static SeverityLevel ParseToSeverity(this MonitorLogDataRecordLevel logLevel)
    {
        switch (logLevel)
        {
            case MonitorLogDataRecordLevel.Debug:
                return SeverityLevel.Verbose;
            case MonitorLogDataRecordLevel.Info:
                return SeverityLevel.Information;
            case MonitorLogDataRecordLevel.Warn:
                return SeverityLevel.Warning;
            case MonitorLogDataRecordLevel.Error:
                return SeverityLevel.Error;
            case MonitorLogDataRecordLevel.Fatal:
                return SeverityLevel.Critical;
            default:
                throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null);
        }
    }
}
public静态类ApplicationInsightsParser
{
公共静态跟踪遥测(此MonitorLogDataRecord日志)
{
返回新的轨迹测量
{
Timestamp=log.Timestamp,
//属性={{“”“”},
上下文=
{
//组成部分=
//{
//Version=“”
//},
//装置=
//{
//Id=“”,
//型号=”,
//OemName=“”,
//操作系统=”,
//Type=“”
//},
//云彩=
//{
//RoleInstance=“”,
//RoleName=“”
//},
//标志=0,
//InstrumentationKey=“”,
//位置=
//{
//Ip=“”
//},
操作=
{
Name=log.Source
//相关向量=”,
//Id=“”,
//ParentId=“”,
//SyntheticSource=“”
}
//会议=
//{
//Id=“”,
//IsFirst=true
//},
//使用者=
//{
//Id=“”,
//AccountId=“”,
//AuthenticatedUserId=“”,
//UserAgent=“”
//},
//GlobalProperty={{{“”“”}
},
//扩展名=null,
//序列=”,
//ProactiveSamplingDecision=SamplingDecision.None,
Message=log.Content,
SeverityLevel=log.Level.ParseToSeverity()
};
}
公共静态异常遥测到异常遥测(此MonitorLogDataRecord日志)
{
返回新的异常遥测
{
Timestamp=log.Timestamp,
//属性={{“”“”},
上下文=
{
//组成部分=
//{
//Version=“”
//},
//装置=
//{
//Id=“”,
//型号=”,
//OemName=“”,
//操作系统=”,
//Type=“”
//},
//云彩=
//{
//RoleInstance=“”,
//RoleName=“”
//},
//标志=0,
//InstrumentationKey=“”,
//位置=
//{
//Ip=“”
//},
操作=
{
Name=log.Source
//相关向量=”,
//Id=“”,
//ParentId=“”,
//SyntheticSource=“”
}
//会议=
//{
//Id=“”,
//IsFirst=true
//},
//使用者=
//{
//Id=“”,
//AccountId=“”,
//AuthenticatedUserId=“”,
//UserAgent=“”
//},
//全球财产=
//{
//    {"", ""}
//}
},
//扩展名=null,
//序列=”,
//ProactiveSamplingDecision=SamplingDecision.None,
//Message=log.Content,
SeverityLevel=log.Level.ParseToSeverity(),
//指标=
//{
//    {"", 0}
//},
Exception=JsonConvert.DeserializeObject(log.SerializedException)
//ProblemId=“”
};
}
私有静态SeverityLevel ParseToSeverity(此MonitorLogDataRecordLevel logLevel)
{
开关(日志级别)
{
案例监视器LogDataRecordLevel.Debug:
返回SeverityLevel.Verbose;
案例监视器LogDataRecordLevel.Info:
返回严重级别信息;
案例监视器LogDataRecordLevel。警告:
返回严重级别警告;
案例监视器LogDataRecordLevel。错误:
返回严重级别错误;
案例监视器LogDataRecordLevel。致命:
返回严重级别临界值;
违约:
抛出新ArgumentOutOfRangeException(nameof(logLevel),logLevel,null);
}
}
}


使用
Startup.cs
将服务实例化为singleton,但即使我们在运行时得到0个错误,并且我们看到正在处理的队列中的消息,在应用程序上搜索内容,我们也找不到任何跟踪或异常。
我们试图强制执行
TrackTrace
TrackExeption
TrackEvent
,测试后我们只能看到
事件。
通过搜索web,我们可以找到您可以看到的配置,但仍然不适用于我们


有什么建议吗?提前感谢您的帮助

我怀疑您可能在host.json文件中配置了
采样设置
日志级别设置

有关采样设置,请参阅和

有关
LogLevel
,您可以参考


如果不是这样,请告诉我。

谢谢您的回答!事实上,我们发现了这个问题,一个
public static class ApplicationInsightsParser
{
    public static TraceTelemetry ToTraceTelemetry(this MonitorLogDataRecord log)
    {
        return new TraceTelemetry
        {
            Timestamp = log.Timestamp,
            //Properties = {{"", ""}},
            Context =
            {
                //Component =
                //{
                //    Version = ""
                //},
                //Device =
                //{
                //    Id = "",
                //    Model = "",
                //    OemName = "",
                //    OperatingSystem = "",
                //    Type = ""
                //},
                //Cloud =
                //{
                //    RoleInstance = "",
                //    RoleName = ""
                //},
                //Flags = 0,
                //InstrumentationKey = "",
                //Location =
                //{
                //    Ip = ""
                //},
                Operation =
                {
                    Name = log.Source
                    //CorrelationVector = "",
                    //Id = "",
                    //ParentId = "",
                    //SyntheticSource = ""
                }
                //Session =
                //{
                //    Id = "",
                //    IsFirst = true
                //},
                //User =
                //{
                //    Id = "",
                //    AccountId = "",
                //    AuthenticatedUserId = "",
                //    UserAgent = ""
                //},
                //GlobalProperties = {{"", ""}}
            },
            //Extension = null,
            //Sequence = "",
            //ProactiveSamplingDecision =SamplingDecision.None,
            Message = log.Content,
            SeverityLevel = log.Level.ParseToSeverity()
        };
    }

    public static ExceptionTelemetry ToExceptionTelemetry(this MonitorLogDataRecord log)
    {
        return new ExceptionTelemetry
        {
            Timestamp = log.Timestamp,
            //Properties = {{"", ""}},
            Context =
            {
                //Component =
                //{
                //    Version = ""
                //},
                //Device =
                //{
                //    Id = "",
                //    Model = "",
                //    OemName = "",
                //    OperatingSystem = "",
                //    Type = ""
                //},
                //Cloud =
                //{
                //    RoleInstance = "",
                //    RoleName = ""
                //},
                //Flags = 0,
                //InstrumentationKey = "",
                //Location =
                //{
                //    Ip = ""
                //},
                Operation =
                {
                    Name = log.Source
                    //CorrelationVector = "",
                    //Id = "",
                    //ParentId = "",
                    //SyntheticSource = ""
                }
                //Session =
                //{
                //    Id = "",
                //    IsFirst = true
                //},
                //User =
                //{
                //    Id = "",
                //    AccountId = "",
                //    AuthenticatedUserId = "",
                //    UserAgent = ""
                //},
                //GlobalProperties =
                //{
                //    {"", ""}

                //}
            },
            //Extension = null,
            //Sequence = "",
            //ProactiveSamplingDecision = SamplingDecision.None,
            //Message = log.Content,
            SeverityLevel = log.Level.ParseToSeverity(),
            //Metrics =
            //{
            //    {"", 0}
            //},
            Exception = JsonConvert.DeserializeObject<Exception>(log.SerializedException)
            //ProblemId = ""
        };
    }

    private static SeverityLevel ParseToSeverity(this MonitorLogDataRecordLevel logLevel)
    {
        switch (logLevel)
        {
            case MonitorLogDataRecordLevel.Debug:
                return SeverityLevel.Verbose;
            case MonitorLogDataRecordLevel.Info:
                return SeverityLevel.Information;
            case MonitorLogDataRecordLevel.Warn:
                return SeverityLevel.Warning;
            case MonitorLogDataRecordLevel.Error:
                return SeverityLevel.Error;
            case MonitorLogDataRecordLevel.Fatal:
                return SeverityLevel.Critical;
            default:
                throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null);
        }
    }
}