1using CoreImportDataApp.Common; 2using Microsoft.Extensions.Configuration; 3using Microsoft.Extensions.DependencyInjection; 4using System; 5using CoreImportDataApp.Services; 6using NLog;//NLog.Extensions.Logging 和NLog.Web.AspNetCore两个类库。 7using Microsoft.Extensions.Logging; 8using Microsoft.AspNetCore.Hosting; 9 10namespace CoreImportDataApp 11{ 12 class Program 13 { 14 public static string SqlConnecting { get; set; } 15 static void Main(string[] args) 16 { 17 /** 18 * 在ASP.NET Core中使用依赖注入中使用很简单,只需在Startup类的ConfigureServices()方法中, 19 * 通过IServiceCollection接口进行注入即可,其它的无需关心 20 * 21 * 在控制台程序中就不一样了,除了注入外,你还需要构建容器,解析注入。 22 * 注入通过IServiceCollection接口,而构建容器需要调用IServiceCollection的扩展方法BuildServiceProvider(), 23 * 解析需要调用IServiceProvider的扩展方法GetService<T>() 24 **/ 25 var builder = new ConfigurationBuilder() 26 .AddJsonFile("appSetting.json"); 27 var configuration = builder.Build(); 28 29 SqlConnecting = configuration.GetConnectionString("DefaultConnection"); 30 31 IServiceCollection services = new ServiceCollection(); 32 services.AddOptions(); 33 services.Configure<TableStoreModel>(configuration.GetSection("TableStore")); 34 services.AddSingleton<TableStoreModel>(); 35 services.AddTransient<ILoggerFactory, LoggerFactory>(); 36 services.AddTransient<ITest, Test>(); 37 38 IServiceProvider serviceProvider = services.BuildServiceProvider(); 39 40 TestDI testDI = new TestDI(serviceProvider); 41 testDI.StartWork(); 42 var host = new WebHostBuilder().UseKestrel().UseStartup<Startup>().Build(); 43 host.Run(); 44 Console.ReadLine(); 45 } 46 } 47} 48 49using Microsoft.AspNetCore.Builder; 50using Microsoft.AspNetCore.Hosting; 51using Microsoft.AspNetCore.Http; 52using Microsoft.Extensions.Logging; 53using NLog.Extensions.Logging; 54using NLog.Web; 55 56namespace CoreImportDataApp 57{ 58 public class Startup 59 { 60 public static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); 61 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 62 { 63 loggerFactory.AddNLog(); 64 env.ConfigureNLog("nlog.config"); 65 app.Run(context=> 66 { 67 return context.Response.WriteAsync("bido-Nlog"); 68 }); 69 } 70 } 71} 72 73using Microsoft.Extensions.Logging; 74using System; 75using System.Collections.Generic; 76using System.Text; 77using NLog; 78using NLog.Extensions.Logging; 79 80namespace CoreImportDataApp.Services 81{ 82 public class Test:ITest 83 { 84 public string Add() 85 { 86 Startup.log.Info("运行中...."); 87 return "ITest => test /add()"; 88 } 89 } 90} 91 92<?xml version="1.0" encoding="utf-8" ?> 93<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 94 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 95 autoReload="true" 96 internalLogLevel="Warn" 97 internalLogFile="c:\temp\internal-nlog.txt"> 98 99 <!-- define various log targets --> 100 <targets> 101 <!-- write logs to file --> 102 <target xsi:type="File" name="allfile" fileName="D:\ProjectCode\C#Test\NetCore\netcoreWebApi\BidoCoreApi\CoreImportDataApp\bin\logs\nlog-all\${shortdate}.log" 103 layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" /> 104 105 106 <target xsi:type="File" name="ownFile-web" fileName="D:\ProjectCode\C#Test\NetCore\netcoreWebApi\BidoCoreApi\CoreImportDataApp\bin\logs\nlog-own\${shortdate}.log" 107 layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}" /> 108 109 <target xsi:type="Null" name="blackhole" /> 110 </targets> 111 112 <rules> 113 <!--All logs, including from Microsoft--> 114 <logger name="*" minlevel="Warn" writeTo="allfile" /> 115 116 <!--Skip Microsoft logs and so log only own logs--> 117 <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> 118 <logger name="*" minlevel="Debug" writeTo="ownFile-web" /> 119 <!--Trace Debug Info Warn ERROR Fatal--> 120 </rules> 121</nlog>
以上是在网上综合找到的结果;
但是总感觉 投机取巧的使用了web端依赖注入的功能;
各位请吐槽。。有什么高见尽管留言,看到后一一回复