public static string Format(this LoggerExtension extension, Exception exception)
        {
            Type type = exception.GetType();
            IExceptionFormatter formatter = Logger.ExceptionFormatters.ContainsKey(type) ? Logger.ExceptionFormatters[type] : Logger.ExceptionFormatters[typeof(Exception)];

            return(formatter.Format(exception));
        }
 public static void TraceJumpBack(this LoggerExtension extension, string message)
 {
     Logger.Trace(message);
     if (Logger.Console.IsConsoleAvailable)
     {
         Console.CursorTop--;
     }
 }
Exemple #3
0
        public static void Main(string[] args)
        {
            #region Serilog configuration
            LoggerExtension logger = new LoggerExtension();
            logger.CreateLogger();

            CreateHostBuilder(args).Build().Run();
            #endregion
        }
        public void GetLogger_ReturnsLogger()
        {
            //Arrange

            //Act
            var logger = LoggerExtension.GetLogger(GetType());

            //Assert
            Assert.That(logger, Is.Not.Null);
            Assert.That(logger, Is.InstanceOf <ILog>());
        }
Exemple #5
0
        static void Main(string[] args)
        {
            LoggerExtension.ConfigureLogger();

            HostFactory.Run(x =>
            {
                x.SetServiceName("Service name");
                x.SetDisplayName("Service display name");
                x.SetDescription("Service description");

                x.RunAsLocalSystem();
                x.UseLog4Net("log4net.config", true);
                x.StartAutomatically();


                x.Service <GreetingServer>(ser =>
                {
                    ser.ConstructUsing(name => new GreetingServer());
                    ser.WhenStarted(sta => sta.Start());
                    ser.WhenStopped(stp => stp.Stop());
                });

                x.EnableServiceRecovery(rec =>
                {
                    rec.OnCrashOnly();
                    rec.RestartService(delayInMinutes: 0);
                    rec.RestartService(delayInMinutes: 1);

                    // Corresponds to ‘Subsequent failures: Restart the Service’
                    rec.RestartService(delayInMinutes: 5);

                    rec.SetResetPeriod(days: 1);
                });

                x.OnException((ex) =>
                {
                    var log = HostLogger.Get <Program>();
                    log.Error("Global exception", ex);
                });
            });
        }
        public override void OnException(HttpActionExecutedContext context)
        {
            //UNCOMMENT AND ADJUST THE CODE BELOW IF THERE IS A NEED FOR SPECIAL TREATMENT FOR CERTAIN TYPE OF EXCEPTION
            //
            //if (context.Exception is BusinessException)
            //{
            //  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
            //  {
            //      Content = new StringContent(context.Exception.Message),
            //      ReasonPhrase = "Exception"
            //  });

            //}

            //LOG THE ERROR
            Debug.WriteLine(context.Exception);

            var exceptionType = context.Exception.GetType();
            var cpLogManager  = LoggerExtension.GetLogger(exceptionType);

            //Send a JSON object
            var items = new Dictionary <string, string>
            {
                { "RequestGUID", GetOwinContext().Get <Guid>("RequestGUID").ToString() },
                { "ActionContext", context.ActionContext?.ToString() ?? "" },
                { "Exception", context.Exception?.ToString() },
                { "Request", context.Request?.ToString() ?? "" },
                { "Response", context.Response?.ToString() ?? "" }
            };

            cpLogManager.Error(items);

            //UNCOMMENT AND ADJUST THE CODE BELOW IF THERE IS A NEED FOR RE-THROWING THE EXCEPTION
            //
            //throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
            //{
            //  Content = new StringContent("An error occurred, please try again or contact the administrator."),
            //  ReasonPhrase = "Critical Exception"
            //});
        }
        public FileAnalyzerService()
        {
            LoggerExtension logger = new LoggerExtension();

            _logger = logger.CreateLogger();
        }