Application Insights配置Web API [英] Application Insights configuration web API

查看:209
本文介绍了Application Insights配置Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个库项目,用于将日志写入ApplicationInsights和表存储中,并且正在使用我的其他其他WebAPI项目.但是由于某些原因,日志没有记录到Application Insights中,但是可以与表存储一起使用.

I have created a library project for writing logs into ApplicationInsights as well as table storage and is being consumed my different other WebAPI projects. But due to some reason the logs are not getting logged in Application Insights but it works with table storage.

private void AddTelemetryTarget(string instrumentationKey, LoggerEnumerations.LogLevel minLogLevel, LoggingConfiguration config)
        {
            try
            {           ConfigurationItemFactory.Default.Targets.RegisterDefinition("ApplicationInsightsTarget", typeof(ApplicationInsightsTarget));
                ApplicationInsightsTarget aiTarget = new ApplicationInsightsTarget();
                aiTarget.InstrumentationKey = instrumentationKey;
                aiTarget.Name = "ai";
                var wrapper = new AsyncTargetWrapper(aiTarget, 5000, AsyncTargetWrapperOverflowAction.Grow);
                config.AddTarget("TelemetryAsyncWrapper", wrapper);

                //Applying logging rules.
                LoggingRule rule = new LoggingRule("*", ConvertLogType(minLogLevel), aiTarget);
                config.LoggingRules.Add(rule);
            }
            catch
            { }
        }
        private LogLevel ConvertLogType(LoggerEnumerations.LogLevel type)
        {
            switch (type)
            {
                case LoggerEnumerations.LogLevel.Error: return LogLevel.Error;
                case LoggerEnumerations.LogLevel.Info: return LogLevel.Info;
                case LoggerEnumerations.LogLevel.Warn: return LogLevel.Warn;
                default: return LogLevel.Trace;
            }
        }


public async Task Log(string message, LoggerEnumerations.LogLevel type, Dictionary<string, string> customParams, Exception ex = null, bool isPayload = false)
        {
            LogEventInfo eventInfo = PopulateEventInfo(message, type, customParams, ex);
            if (!isPayload)
            {
                _logger.Log(eventInfo);
            }
            else
            {
                _payloadLogger.Log(eventInfo);
            }
        }

        private LogEventInfo PopulateEventInfo(string message, LoggerEnumerations.LogLevel type, Dictionary<string, string> customParams, Exception ex = null)
        {
            LogEventInfo eventInfo = new LogEventInfo();

            eventInfo.Level = ConvertLogType(type);
            eventInfo.Message = message;
            eventInfo.LoggerName = this.GetType().ToString();
            if (ex != null)
            {
                eventInfo.Exception = ex;
            }
            else if (eventInfo.Level == LogLevel.Error)
            {
                eventInfo.Exception = new Exception(message);
            }
            //Adding custom properties to LogEventInfo to display in Application insight 
            if (customParams != null)
            {
                foreach (KeyValuePair<string, string> param in customParams)
                {
                    eventInfo.Properties.Add(param.Key, param.Value);
                }
            }
            return eventInfo;
        }

Nuget软件包的版本为

Version of Nuget packages are

Microsoft.ApplicationInsights.NLogTarget:2.13.1 NLog:4.6.8

Microsoft.ApplicationInsights.NLogTarget : 2.13.1 NLog : 4.6.8

谢谢

推荐答案

我将Application Insights添加为Connected Services,并从ApplicationInsights.config文件中删除了Instrumentation密钥,并且在注册nlog目标时,我从web.config中使用了Instrumentation密钥文件,它开始工作.

I added Application Insights as Connected Services and I removed the Instrumentation Key from ApplicationInsights.config file and when registering the nlog target I used instrumentation key from my web.config file and it started working.

这篇关于Application Insights配置Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆