Exemplo n.º 1
0
        public void OnException(ExceptionContext context)
        {
            HttpStatusCode status   = HttpStatusCode.InternalServerError;
            String         message  = String.Empty;
            HttpResponse   response = context.HttpContext.Response;

            response.ContentType = "application/json";

            var exceptionType = context.Exception.GetType();

            IEasyLogger logger = logService.GetLogger <Program>();


            context.ExceptionHandled = true;

            if (exceptionType == typeof(OperationException))
            {
                message = ((OperationException)context.Exception).ErrorMessage;
                status  = (((OperationException)context.Exception).ErrorStatus == 400) ? HttpStatusCode.BadRequest : HttpStatusCode.InternalServerError;

                dynamic json = new JObject();
                json.ErrorMessage = message;
                json.ErrorStatus  = status;
                json.ErrorCode    = ((OperationException)context.Exception).ErrorCode;

                response.StatusCode = (int)status;

                var err = ((JObject)json).ToString();

                logger.Error(err);

                response.WriteAsync(err);
            }
            else
            {
                if (exceptionType == typeof(UnauthorizedAccessException))
                {
                    message = "Unauthorized Access";
                    status  = HttpStatusCode.Unauthorized;
                }
                else if (exceptionType == typeof(NotImplementedException))
                {
                    message = "A server error occurred.";
                    status  = HttpStatusCode.NotImplemented;
                }
                else
                {
                    message = context.Exception.Message;
                    status  = HttpStatusCode.NotFound;
                }

                response.StatusCode = (int)status;
                var err = message;

                logger.Error(err);

                response.WriteAsync(err);
            }
        }
Exemplo n.º 2
0
        internal ScopedLogger(IEasyLogger logger, string scopeName, EasyLogLevel level)
        {
            _scopeName = scopeName;
            _logger    = logger;
            _level     = level;

            Log(string.Concat(OpenLeft, _scopeName, OpenRight));
        }
Exemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IEasyLogger logger = logService.GetLogger <Program>();

            logger.Debug("Configuring Services");

            services.AddMvc(
                config => { config.Filters.Add(typeof(CustomExceptionFilter)); });
        }
Exemplo n.º 4
0
        public Program(TimeSpan duration)
        {
            var configFile = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "the-log4net.config"));

            Log4NetService.Instance.Configure(configFile);

            _duration = duration;
            _logger   = Log4NetService.Instance.GetLogger <Program>();
        }
Exemplo n.º 5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            IEasyLogger logger = logService.GetLogger <Program>();

            logger.Debug("Configuring MVC and Starting Application");

            app.UseStatusCodePages();
            app.UseMvc();
        }
Exemplo n.º 6
0
        public void SetUp()
        {
            _mockedInnerLogger = new Mock <ILogger>();
            _mockedInnerLogger.Setup(l => l.Name).Returns("Inner Logger");

            _mockedLogger = new Mock <ILog>();
            _mockedLogger.Setup(l => l.Logger).Returns(_mockedInnerLogger.Object);
            _mockedInnerLogger.Setup(l => l.IsEnabledFor(Level.Trace)).Returns(true);
            _mockedInnerLogger.Setup(l => l.IsEnabledFor(Level.Debug)).Returns(true);
            _mockedInnerLogger.Setup(l => l.IsEnabledFor(Level.Info)).Returns(true);
            _mockedInnerLogger.Setup(l => l.IsEnabledFor(Level.Warn)).Returns(true);
            _mockedInnerLogger.Setup(l => l.IsEnabledFor(Level.Error)).Returns(true);
            _mockedInnerLogger.Setup(l => l.IsEnabledFor(Level.Fatal)).Returns(true);

            _logger = new Log4NetLogger(_mockedLogger.Object);
            _logger.Name.ShouldBe("Inner Logger");
        }
Exemplo n.º 7
0
        public void Run()
        {
            var logger = _logService.GetLogger(GetType());

            logger.Debug("Something is about to happen...");

            // Should not be replaced by Task.Delay as
            // Thread name will be changed after await
            // which will fail the log content assertions
            Thread.Sleep(TimeSpan.FromSeconds(1.5));

            logger.InfoFormat("What's your number? It's: {0}", 1234.ToString());

            logger.Error("Ooops I did it again!", new ArgumentNullException("cooCoo", "Parameter cannot be null."));

            logger.FatalFormat("Going home now - {0}", new ApplicationException("CiaoCiao"));

            Thread.Sleep(TimeSpan.FromSeconds(1.5));

            using (logger.GetScopedLogger("[Scope 1]"))
            {
                logger.Debug("This should be inside scope 1.");

                using (logger.GetScopedLogger("[Scope 2]"))
                {
                    logger.WarnFormat("This should be inside scope 1 and scope 2. {0}", "Bar");
                }

                logger.Error("This should still be inside scope 1.");
            }

            logger.Fatal("This should not be inside any scope.");

            Thread.Sleep(TimeSpan.FromSeconds(1.5));

            IEasyLogger anotherLogger = _logService.GetLogger <Context>();

            anotherLogger.ShouldNotBeNull();
            anotherLogger.Name.ShouldBe("Easy.Logger.Tests.Integration.Context");
        }
Exemplo n.º 8
0
 /// <summary>
 /// Creates an instance of the <see cref="EasyLogger"/>.
 /// </summary>
 /// <param name="easyLogger">An instance of the <see cref="IEasyLogger"/>.</param>
 public EasyLogger(IEasyLogger easyLogger) => _logger = easyLogger;
Exemplo n.º 9
0
 public Program(TimeSpan duration)
 {
     _duration = duration;
     _logger   = Log4NetService.Instance.GetLogger <Program>();
 }
Exemplo n.º 10
0
 /// <summary>
 /// Returns a <see cref="ScopedLogger"/> which provides ability to mark the
 /// begin and end of a scope defined by <paramref name="name"></paramref>.
 /// </summary>
 /// <param name="logger">The logger</param>
 /// <param name="name">The name of the scope</param>
 /// <param name="level">The level at which the scope should be logged</param>
 /// <returns>The scoped logger</returns>
 public static ScopedLogger GetScopedLogger(this IEasyLogger logger, string name, EasyLogLevel level) =>
 new ScopedLogger(logger, name, level);