Net Core 控制台程序使用Nlog 输出到log文件

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端依赖注入的功能;

各位请吐槽。。有什么高见尽管留言,看到后一一回复

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )