Exemplo n.º 1
0
        // This code configures Web API. The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public void Configuration(IAppBuilder appBuilder)
        {
            ConfigureWebApi(appBuilder);

            appBuilder.Use(async(env, next) =>
            {
                FormatLoggerAccessor.Locate().Debug($"{env.Request.Method} {env.Request.Path}");
                await next();
                FormatLoggerAccessor.Locate().Debug($"Response code: {env.Response.StatusCode}");
            });

            // StructureMap:
            // config.DependencyResolver = ServiceLocator.Get<StructureMapWebApiDependencyResolver>();

            // Elmah
            // config.Services.Add(typeof(IExceptionLogger), new ElmahWebApi2ExceptionLogger());

            // Make ./public the default root of the static files in our Web Application.
            //app.UseFileServer(new FileServerOptions
            //{
            //    RequestPath = new PathString(string.Empty),
            //    FileSystem = new PhysicalFileSystem("./public"),
            //    EnableDirectoryBrowsing = true,
            //});
        }
Exemplo n.º 2
0
        public FormatLoggerTestFixture()
        {
            var mock = Substitute.For <ILog>();

            mock.When(x => x.Debug(Arg.Any <string>()))
            .Do(c =>
            {
                LogOutput("debug", c);
            });

            mock.When(x => x.Info(Arg.Any <string>()))
            .Do(c =>
            {
                LogOutput("info", c);
            });

            mock.When(x => x.Warn(Arg.Any <string>()))
            .Do(c =>
            {
                LogOutput("warn", c);
            });

            mock.When(x => x.Error(Arg.Any <string>()))
            .Do(c =>
            {
                LogOutput("error", c);
            });

            FormatLoggerAccessor.Initialize(() => mock);

            void LogOutput(string level, CallInfo callInfo)
            {
                TestOutputHelper?.WriteLine($"{level.ToUpper()}: {callInfo.Arg<string>()}");
            }
        }
        private static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {
            FormatLoggerAccessor.Locate().Error("Global exception handler error", (Exception)e.ExceptionObject);

            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }
        }
Exemplo n.º 4
0
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var content = AsyncHelper.RunSync(() => actionExecutedContext.Request.Content.ReadAsStringAsync());

            FormatLoggerAccessor.Locate().Error(actionExecutedContext.Exception,
                                                $"\r\n{actionExecutedContext.Request.Method} {actionExecutedContext.Request.RequestUri}\r\n" +
                                                $"{actionExecutedContext.Request.Headers.ToLogString()}" +
                                                $" {content}\r\n");
        }
 static void Main(string[] args)
 {
     JsonConfigAccessor.Initialize();
     FormatLoggerAccessor.Initialize(() => LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType));
     AppDomain.CurrentDomain.UnhandledException += GlobalExceptionHandler;
     HostFactory.Run(x =>
     {
         x.Service <IService>(s =>
         {
             s.ConstructUsing(name => new WindowsService());
             s.WhenStarted(tc => tc.Start());
             s.WhenStopped(tc => tc.Stop());
         });
         x.RunAsLocalSystem();
         x.UseLog4Net("log4net.config");
         x.SetDescription("Project template for Web API OWIN service application.");
         x.SetDisplayName("WebApiOwinBoilerplate service");
         x.SetServiceName("WebApiOwinBoilerplateService");
     });
 }
Exemplo n.º 6
0
 public void Should_show_output_in_test_run()
 {
     FormatLoggerAccessor.Locate().Error($"An error");
 }