Beispiel #1
0
        static void Main()
        {
            // Example of code-only setup, alteratively this can be in the App.config
            // rollupSeconds is 0 so a new file is always generated, for demonstration purposes
            ErrorStore.Setup("Samples.Console", new JSONErrorStore(path: "Errors", rollupSeconds: 0));

            // Optional: for logging all unhandled exceptions
            AppDomain.CurrentDomain.UnhandledException += ExceptionalHandler;

            DisplayExceptionStats();
            PauseForInput();

            try
            {
                throw new Exception("Just a try/catch test");
            }
            catch (Exception ex)
            {
                // logged, but caught so we don't crash
                ErrorStore.LogExceptionWithoutContext(ex);
            }

            DisplayExceptionStats();
            PauseForInput();

            System.Console.WriteLine("This next one will crash the program, but will be logged on the way out...");
            PauseForInput();

            // one not explicitly caught, will be logged by ExceptionHandler
            throw new Exception("I am an exception thrown on exit");
        }
Beispiel #2
0
        protected override void Write(LogEventInfo logEvent)
        {
            var exception = logEvent.Exception;

            // Only capture log events that include an exception.
            if (exception == null)
            {
                return;
            }

            var logMessage = logEvent.FormattedMessage;
            var logLevel   = logEvent.Level.ToString().ToUpperInvariant();
            var loggerName = !string.IsNullOrEmpty(logEvent.LoggerName) ? logEvent.LoggerName : "<empty>";

            var logData = new Dictionary <string, string>
            {
                { "NLog-Level", logLevel },
                { "NLog-LoggerName", loggerName }
            };

            if (!string.IsNullOrEmpty(logMessage))
            {
                logData.Add("NLog-Message", logMessage);
            }

            ErrorStore.LogExceptionWithoutContext(exception, customData: logData);
        }
Beispiel #3
0
 /// <summary>
 /// manually write an exception to our standard exception log
 /// </summary>
 public static void LogException(Exception ex, string key = null, int?reLogDelaySeconds = null)
 {
     if (!ShouldLog(key))
     {
         return;
     }
     ErrorStore.LogExceptionWithoutContext(ex, appendFullStackTrace: true);
     RecordLogged(key);
 }
        // Optional, for logging all unhanled exceptions on the way out
        static void ExceptionalHandler(object sender, UnhandledExceptionEventArgs e)
        {
            // e.ExceptionObject may not be an exception, refer to http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf
            // section 10.5, CLS Rule 40 if you're curious on why this check needs to happen
            var exception = e.ExceptionObject as Exception;

            if (exception != null)
            {
                ErrorStore.LogExceptionWithoutContext(exception);
            }
        }
Beispiel #5
0
        private static void ErrorHandle(Exception ex, string feedUrl)
        {
            // we can send an email for warning right here if needed

            // or just log error into some error stores, it's up to you
            ErrorStore.LogExceptionWithoutContext(ex, false, false,
                                                  new Dictionary <string, string>
            {
                { "feedUrl", feedUrl }
            });
        }
        public static void ApplicationStart()
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;

            try
            {
                SqlSettings.AutoQuotedIdentifiers = true;
                Serenity.Web.CommonInitialization.Run();

                var registrar = Dependency.Resolve <IDependencyRegistrar>();
                registrar.RegisterInstance <IAuthorizationService>(new Administration.AuthorizationService());
                registrar.RegisterInstance <IAuthenticationService>(new Administration.AuthenticationService());
                registrar.RegisterInstance <IPermissionService>(new LogicOperatorPermissionService(new Administration.PermissionService()));
                registrar.RegisterInstance <IUserRetrieveService>(new Administration.UserRetrieveService());

                if (!ConfigurationManager.AppSettings["LDAP"].IsTrimmedEmpty())
                {
                    registrar.RegisterInstance <IDirectoryService>(new LdapDirectoryService());
                }

                if (!ConfigurationManager.AppSettings["ActiveDirectory"].IsTrimmedEmpty())
                {
                    registrar.RegisterInstance <IDirectoryService>(new ActiveDirectoryService());
                }

                InitializeExceptionLog();
                InitializeAppCfg();
                InitializeDataAccessHelpers();

                // clean up FeedItems (for duplicate title in chanel)
                Task.Run(() =>
                {
                    try
                    {
                        SimpleFeedlyDatabaseAccess.CleanupFeedItemsData();
                    }
                    catch (Exception ex)
                    {
                        ErrorStore.LogExceptionWithoutContext(ex);
                    }
                });


                // we don't run RssCrawler in web application
                // we will use a Windows Service: SimpleFeedly.Crawler
                //InitializeRssCrawler(AppSettings.Crawler.ChannelFetchingDelay, AppSettings.Crawler.ChannelErrorDelay, AppSettings.Crawler.ErrorDelay, AppSettings.Crawler.LoopDelay);
            }
            catch (Exception ex)
            {
                ex.Log();
                throw;
            }
        }
 public static void ReportException(Exception x)
 {
     State.Status = "Error";
     State.ErrorDetails.Add(new AppError {
         ErrorType = x.GetType().Name, Message = x.Message, StackTrace = x.StackTrace
     });
     State.HasErrorsSinceLastGeneration = true;
     log.Error(x);
     ErrorStore.LogExceptionWithoutContext(x);
     NewRelic.Api.Agent.NewRelic.NoticeError(x);
 }
Beispiel #8
0
        private static void ConfigExceptionHandling(Config config)
        {
            ErrorStore.Setup(config.GetString("service.name"),
                             new JSONErrorStore(config.GetString("service.exceptions-path"), 200));

            AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
            {
                if (isLoggingException)
                {
                    return;
                }
                using (new DelegateDisposable(() => isLoggingException = true, () => isLoggingException = false))
                {
                    var ex = e.ExceptionObject as Exception;
                    if (ex != null)
                    {
                        try
                        {
                            ErrorStore.LogExceptionWithoutContext(ex);
                        }
                        catch (Exception)
                        {
                            Log.Fatal(ex);
                        }
                    }
                }
            };

            AppDomain.CurrentDomain.FirstChanceException += (sender, e) =>
            {
                if (isLoggingException)
                {
                    return;
                }
                using (new DelegateDisposable(() => isLoggingException = true, () => isLoggingException = false))
                {
                    var ex = e.Exception;
                    if (ex != null)
                    {
                        try
                        {
                            ErrorStore.LogExceptionWithoutContext(ex);
                        }
                        catch (Exception)
                        {
                            Log.Info(ex);
                        }
                    }
                }
            };
        }
Beispiel #9
0
        /// <summary>
        /// Manually write an exception to our standard exception log.
        /// </summary>
        /// <param name="exception">The <see cref="Exception"/> to log.</param>
        /// <param name="key">(Optional) The throttle cache key.</param>
        public static void LogException(Exception exception, string key = null)
        {
            if (!ShouldLog(key))
            {
                return;
            }

            if (exception is DeserializationException deserializationException)
            {
                exception.AddLoggedData("Snippet-After", deserializationException.SnippetAfterError)
                .AddLoggedData("Position", deserializationException.Position.ToString())
                .AddLoggedData("Ended-Unexpectedly", deserializationException.EndedUnexpectedly.ToString());
            }

            ErrorStore.LogExceptionWithoutContext(exception, appendFullStackTrace: true);
            RecordLogged(key);
        }
Beispiel #10
0
        public TestChannelResponse TestChannel(IDbConnection connection, TestChannelRequest request)
        {
            try
            {
                request.CheckNotNull();

                if (!StringUtils.IsUrl(request.FeedUrl))
                {
                    throw new Exception($"Url '{request.FeedUrl}' is invalid");
                }

                var feed = RssCrawler.GetFeedsFromChannel(request.FeedUrl, null, false, out RssCrawlerEngine usedEngine, out Exception fetchError);

                if (feed == null || feed.Items == null || feed.Items.Count == 0)
                {
                    if (fetchError != null)
                    {
                        ErrorStore.LogExceptionWithoutContext(fetchError, false, false,
                                                              new Dictionary <string, string>
                        {
                            { "channelId", request.FeedUrl },
                            { "engine", usedEngine.ToString() }
                        });
                    }
                    throw new Exception("Cannot fetch data");
                }
                else
                {
                    return(new TestChannelResponse
                    {
                        Entities = feed.Items.Select(x => new MyRow
                        {
                            Title = x.Title,
                            Link = x.Link,
                            Description = x.Description
                        }).ToList(),
                        Engine = usedEngine.ToString()
                    });
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Hi, we have an error: " + ex?.Message ?? string.Empty);
            }
        }
        private static void LogToExceptional(Exception error, string message = null, Dictionary <string, string> customData = null)
        {
            if (error == null)
            {
                return;
            }

            if (customData == null)
            {
                customData = new Dictionary <string, string>();
            }

            if (message != null)
            {
                customData["Message"] = message;
            }

            ErrorStore.LogExceptionWithoutContext(error, customData: customData);
        }