|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Builder; |
| 6 | +using Microsoft.AspNetCore.Hosting; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using System.Text; |
| 11 | +using NLog.Extensions.Logging; |
| 12 | + |
| 13 | +namespace NETCoreLogging |
| 14 | +{ |
| 15 | + public class Startup |
| 16 | + { |
| 17 | + public Startup(IHostingEnvironment env) |
| 18 | + { |
| 19 | + var builder = new ConfigurationBuilder() |
| 20 | + .SetBasePath(env.ContentRootPath) |
| 21 | + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
| 22 | + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) |
| 23 | + .AddEnvironmentVariables(); |
| 24 | + Configuration = builder.Build(); |
| 25 | + } |
| 26 | + |
| 27 | + public IConfigurationRoot Configuration { get; } |
| 28 | + |
| 29 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 30 | + public void ConfigureServices(IServiceCollection services) |
| 31 | + { |
| 32 | + // Add framework services. |
| 33 | + services.AddMvc(); |
| 34 | + } |
| 35 | + |
| 36 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 37 | + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
| 38 | + { |
| 39 | + loggerFactory.AddNLog();//添加NLog |
| 40 | + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
| 41 | + loggerFactory.AddConsole(Configuration.GetSection("Logging")); |
| 42 | + loggerFactory.AddDebug(); |
| 43 | + |
| 44 | + if (env.IsDevelopment()) |
| 45 | + { |
| 46 | + app.UseDeveloperExceptionPage(); |
| 47 | + app.UseBrowserLink(); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + app.UseExceptionHandler("/Home/Error"); |
| 52 | + } |
| 53 | + |
| 54 | + app.UseStaticFiles(); |
| 55 | + |
| 56 | + app.UseMvc(routes => |
| 57 | + { |
| 58 | + routes.MapRoute( |
| 59 | + name: "default", |
| 60 | + template: "{controller=Home}/{action=Index}/{id?}"); |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments