示例#1
0
        public void Configure(IApplicationBuilder app)
        {
            ServiceProvider = app.ApplicationServices;

            var applicationEnvironment = app.ApplicationServices.GetRequiredService <IApplicationEnvironment>();
            var configurationFile      = Path.Combine(applicationEnvironment.ApplicationBasePath, applicationEnvironment.ApplicationName) + ".json";

            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddJsonFile(configurationFile);

            Configuration = configurationBuilder.Build();

            ConfigureLogging(app);

            // Add site logging.
            app.Use(async(request, next) => {
                var accessLine = "<Unknown>";

                try {
                    var remoteAddress     = "";
                    var connectionFeature = request.GetFeature <IHttpConnectionFeature>();

                    if (connectionFeature != null)
                    {
                        remoteAddress = connectionFeature.RemoteIpAddress.ToString();
                    }

                    if (string.IsNullOrWhiteSpace(remoteAddress))
                    {
                        remoteAddress = request.Request.Headers["HTTP_X_FORWARDED_FOR"];
                    }

                    if (string.IsNullOrWhiteSpace(remoteAddress))
                    {
                        remoteAddress = request.Request.Headers["REMOTE_ADDR"];
                    }

                    accessLine = string.Format(
                        "{0} {1} {2} {3}{4}{5}",
                        remoteAddress,
                        request.Request.Method,
                        request.Request.Protocol,
                        request.Request.Path,
                        request.Request.QueryString.HasValue ? "?" : "",
                        request.Request.QueryString);

                    var isHtml = Path.GetExtension(request.Request.Path).Equals(".html");

                    if (isHtml)
                    {
                        HtmlAccessLog.LogInformation(accessLine);
                    }

                    AccessLog.LogInformation(accessLine);

                    await next();
                } catch (Exception e) {
                    var message = string.Format("Exception processing request {0}", accessLine);
                    ApplicationLog.LogError(message, e);
                }
            });

            ConfigureFileServer(app);
        }