Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        protected virtual void ConfigureLogging(IApplicationBuilder app)
        {
            var hostingEnvironment     = app.ApplicationServices.GetRequiredService <IHostingEnvironment>();
            var applicationEnvironment = app.ApplicationServices.GetRequiredService <IApplicationEnvironment>();
            var loggerFactory          = app.ApplicationServices.GetRequiredService <LoggerFactory>();

            var nlogConfigFileGenerated = false;
            var nlogConfigFilename      = "NLog.config";
            var nlogConfigSearchPaths   = new string[] {
                ".",
                applicationEnvironment.ApplicationBasePath
            };

            var nlogConfigFilePath = PathUtils.FindPath(nlogConfigSearchPaths, nlogConfigFilename);

            // If no NLog.config file is found, generate one to use.
            // The stub is created so that admins can edit and configure
            // logging even in cases where a file was not provided.
            if (nlogConfigFilePath == null)
            {
                nlogConfigFileGenerated = true;
                nlogConfigFilePath      = Path.Combine(applicationEnvironment.ApplicationBasePath, "NLog.config");
                File.WriteAllText(nlogConfigFilePath, @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<nlog
	xmlns=""http://www.nlog-project.org/schemas/NLog.xsd""
	xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
	autoReload=""true""
>
	<targets>
		<target
			name=""Console""
			xsi:type=""ColoredConsole""
			layout=""[${pad:padding=7:inner=${level:uppercase=true}}] ${logger}: ${message}""/>
	</targets>

	<rules>
		<logger name=""*"" minlevel=""Info"" writeTo=""console"" />
	</rules>
</nlog>
");
            }

            var nlogFactory = new NLog.LogFactory(new XmlLoggingConfiguration(nlogConfigFilePath)
            {
                AutoReload = true
            });

            loggerFactory.AddNLog(nlogFactory);

            ApplicationLog = loggerFactory.CreateLogger("Application");
            ApplicationLog.LogInformation("Log file opened at {0}.", DateTime.Now);

            if (nlogConfigFileGenerated)
            {
                ApplicationLog.LogWarning("NLog configuration file could not be found. A new one has been generated.");
            }

            ApplicationLog.LogInformation("Logging configuration file: {0}", nlogConfigFilePath);

            AccessLog = loggerFactory.CreateLogger("Access");
            AccessLog.LogInformation("Log file opened at {0}.", DateTime.Now);

            HtmlAccessLog = loggerFactory.CreateLogger("HtmlAccess");
            HtmlAccessLog.LogInformation("Log file opened at {0}.", DateTime.Now);
        }