Exemplo n.º 1
0
        /// <summary>
        /// Shuts down the Application Insights Logging functionality
        /// and flushes any pending requests.
        ///
        /// This handles start and stop times and the application lifetime
        /// log entry that logs duration of operation.
        /// </summary>
        public static void ShutdownLogging()
        {
            if (Configuration.System.SendTelemetry &&
                Telemetry.UseApplicationInsights &&
                AppInsights != null)
            {
                var t = AppRunTelemetry.Telemetry;

                // multi-instance shutdown - ignore
                if (t.Properties.ContainsKey("usage"))
                {
                    return;
                }

                t.Properties.Add("usage", Configuration.ApplicationUpdates.AccessCount.ToString());
                t.Properties.Add("registered", UnlockKey.IsAppRegistered().ToString());
                t.Properties.Add("version", GetVersion());
                t.Properties.Add("dotnetversion", MarkdownMonster.Utilities.mmWindowsUtils.GetDotnetVersion());
                t.Properties.Add("culture", CultureInfo.CurrentUICulture.IetfLanguageTag);
                t.Stop();

                try
                {
                    AppInsights.StopOperation(AppRunTelemetry);
                }
                catch (Exception ex)
                {
                    LogLocal("Failed to Stop Telemetry Client: " + ex.GetBaseException().Message);
                }

                AppInsights.Flush();
                AppInsights = null;
                AppRunTelemetry.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Logs messages to the standard log output for Markdown Monster:
        /// 
        /// * Application Insights
        /// * Local Log File
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public static void Log(string msg, Exception ex = null, bool unhandledException = false, LogLevels logLevel = LogLevels.Error)
        {
            string version = GetVersion();
            string winVersion = null;
            
            if (Telemetry.UseApplicationInsights && AppRunTelemetry?.Telemetry != null)
            {
                var secs = (int) DateTimeOffset.UtcNow.Subtract(AppRunTelemetry.Telemetry.Timestamp).TotalSeconds;

                if (ex != null)
                {
                    AppRunTelemetry.Telemetry.Success = false;
                    AppInsights.TrackException(ex,
                        new Dictionary<string, string>
                        {
                            {"msg", msg},
                            {"exmsg", ex.Message},
                            {"exbasemsg", ex.GetBaseException().Message},
                            {"exsource", ex.Source},
                            {"extrace", ex.StackTrace},
                            {"severity", unhandledException ? "unhandled" : ""},
                            {"version", version},
                            {"winversion", winVersion},
                            {"dotnetversion", MarkdownMonster.Utilities.mmWindowsUtils.GetDotnetVersion()},
                            {"usage", Configuration.ApplicationUpdates.AccessCount.ToString()},
                            {"registered", UnlockKey.IsAppRegistered().ToString()},
                            {"culture", CultureInfo.CurrentCulture.IetfLanguageTag},
                            {"uiculture", CultureInfo.CurrentUICulture.IetfLanguageTag},
                            {"seconds", secs.ToString() },
                            {"level", ((int) logLevel).ToString() + " - " + logLevel.ToString()}
                        });

                }
                else
                {
                    // message only
                    var props = new Dictionary<string, string>()
                    {
                        {"msg", msg},
                        {"usage", Configuration.ApplicationUpdates.AccessCount.ToString()},
                        {"registered", UnlockKey.IsAppRegistered().ToString()},
                        {"version", GetVersion()},
                        {"culture", CultureInfo.CurrentCulture.IetfLanguageTag},
                        {"uiculture", CultureInfo.CurrentUICulture.IetfLanguageTag},
                        {"seconds", secs.ToString() },
                        {"level", ((int) logLevel).ToString() + " - " + logLevel.ToString() }
                    };
                    AppInsights.TrackTrace(msg, props);                    
                }
            }
            
            // also log to the local error log
            LogLocal(msg,ex);            
        }
Exemplo n.º 3
0
        /// <summary>
        /// Starts the Application Insights logging functionality
        /// Note: this should be set on application startup once
        /// and will not fire multiple times.
        /// </summary>
        public static void InitializeLogging()
        {
            try
            {
                if (Configuration.System.SendTelemetry &&
                    Telemetry.UseApplicationInsights &&
                    AppInsights == null)
                {
                    AppInsights = new TelemetryClient
                    {
                        InstrumentationKey = Telemetry.Key
                    };
                    AppInsights.Context.Session.Id        = Guid.NewGuid().ToString();
                    AppInsights.Context.Component.Version = GetVersion();

                    AppRunTelemetry =
                        AppInsights.StartOperation <RequestTelemetry>(
                            $"{GetVersion()} - {Configuration.ApplicationUpdates.AccessCount + 1} - {(UnlockKey.IsAppRegistered() ? "registered" : "unregistered")}");
                    AppRunTelemetry.Telemetry.Start();
                }
            }
            catch (Exception ex)
            {
                Telemetry.UseApplicationInsights = false;
                LogLocal("Application Insights initialization failure: " + ex.GetBaseException().Message);
            }
        }