You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Logging.Configuration;
|
|
using Microsoft.Extensions.Logging.Console;
|
|
using System.IO;
|
|
|
|
namespace LogStudy.WebApp
|
|
{
|
|
/// <summary>
|
|
/// 使用日志:
|
|
/// 1、构造函数注入
|
|
/// 2、使用 LoggerFactory 来创建 ILogger
|
|
/// </summary>
|
|
public class Startup
|
|
{
|
|
/// <summary>
|
|
/// 构建函数注入
|
|
/// </summary>
|
|
private readonly ILogger _logger;
|
|
|
|
/// <summary>
|
|
/// 工厂方法创建
|
|
/// </summary>
|
|
private readonly ILogger _logger2;
|
|
public Startup(IConfiguration configuration,ILogger<Startup> logger)
|
|
{
|
|
Configuration = configuration;
|
|
_logger=logger;
|
|
_logger2=GetLoggerFromFactory("Startup");
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
_logger.LogInformation("配置服务开始");
|
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
|
|
|
_logger2.LogInformation("配置服务结束");
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
|
|
app.UseMvc();
|
|
}
|
|
|
|
private ILogger GetLoggerFromFactory(string name)
|
|
{
|
|
var fac=new LoggerFactory();
|
|
|
|
var logger = fac.AddConsole().CreateLogger(name);
|
|
|
|
return logger;
|
|
}
|
|
}
|
|
}
|