1. using CoreImportDataApp.Common;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using System;
  5. using CoreImportDataApp.Services;
  6. using NLog;//NLog.Extensions.Logging 和NLog.Web.AspNetCore两个类库。
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.AspNetCore.Hosting;
  9.  
  10. namespace 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. }
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Logging;
  5. using NLog.Extensions.Logging;
  6. using NLog.Web;
  7.  
  8. namespace CoreImportDataApp
  9. {
  10. public class Startup
  11. {
  12. public static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  13. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  14. {
  15. loggerFactory.AddNLog();
  16. env.ConfigureNLog("nlog.config");
  17. app.Run(context=>
  18. {
  19. return context.Response.WriteAsync("bido-Nlog");
  20. });
  21. }
  22. }
  23. }
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using NLog;
  6. using NLog.Extensions.Logging;
  7.  
  8. namespace CoreImportDataApp.Services
  9. {
  10. public class Test:ITest
  11. {
  12. public string Add()
  13. {
  14. Startup.log.Info("运行中....");
  15. return "ITest => test /add()";
  16. }
  17. }
  18. }
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. autoReload="true"
  5. internalLogLevel="Warn"
  6. internalLogFile="c:\temp\internal-nlog.txt">
  7.  
  8. <!-- define various log targets -->
  9. <targets>
  10. <!-- write logs to file -->
  11. <target xsi:type="File" name="allfile" fileName="D:\ProjectCode\C#Test\NetCore\netcoreWebApi\BidoCoreApi\CoreImportDataApp\bin\logs\nlog-all\${shortdate}.log"
  12. layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
  13.  
  14. <target xsi:type="File" name="ownFile-web" fileName="D:\ProjectCode\C#Test\NetCore\netcoreWebApi\BidoCoreApi\CoreImportDataApp\bin\logs\nlog-own\${shortdate}.log"
  15. layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}" />
  16.  
  17. <target xsi:type="Null" name="blackhole" />
  18. </targets>
  19.  
  20. <rules>
  21. <!--All logs, including from Microsoft-->
  22. <logger name="*" minlevel="Warn" writeTo="allfile" />
  23.  
  24. <!--Skip Microsoft logs and so log only own logs-->
  25. <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
  26. <logger name="*" minlevel="Debug" writeTo="ownFile-web" />
  27. <!--Trace Debug Info Warn ERROR Fatal-->
  28. </rules>
  29. </nlog>

以上是在网上综合找到的结果;

但是总感觉 投机取巧的使用了web端依赖注入的功能;

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

Net Core 控制台程序使用Nlog 输出到log文件的更多相关文章

  1. 如何在.NET Core控制台程序中使用依赖注入

    背景介绍 依赖注入(Dependency Injection), 是面向对象编程中的一种设计原则,可以用来减低代码之间的耦合度.在.NET Core MVC中 我们可以在Startup.cs文件的Co ...

  2. 在.NET Core控制台程序中使用依赖注入

    之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...

  3. .Net Core 控制台程序错误:Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'.

    .Net Core 控制台程序错误:Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible wi ...

  4. NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions

    .NET Core 控制台程序没有 ASP.NET Core 的 IWebHostBuilder 与 Startup.cs ,那要读 appsettings.json.注依赖.配日志.设 IOptio ...

  5. VisualStudioCode创建的asp.net core控制台程序部署到linux

    1.asp.net core控制台程序 static void Main(string[] args) { ; ) { Console.WriteLine("Hello World!&quo ...

  6. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful) .net core 控制台程序使用依赖注入(Autofac)

    大比速:remoting.WCF(http).WCF(tcp).WCF(RESTful).asp.net core(RESTful) 近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只 ...

  7. 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像

    Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...

  8. Supervisor守护DotNet Core控制台程序

    Supervisor 相信对Linux系统很熟的都知道这个软件,基于Python写的一个守护进程软件.具体的介绍和使用我就不再赘述了. 使用asp.net core 部署在Linux常用的方法 我们可 ...

  9. Mac/Windows开发跨平台.NET Core 控制台程序

    自从微软开始在Github上开源搞.NET Core后,.NET的跨平台逐渐就成真了.多年使用各种语言,说实话还是csharp用起来最舒服.不过现在的工作环境里使用它的机会比较少,大部分时候只是用来写 ...

随机推荐

  1. identity方式

    identity方式      <generator class="identity"/>identity方式表示数据库的主键生成方式为采用数据库的主键生成机制,例如S ...

  2. io学习-相关文章

    文章:IO编程 地址:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143 ...

  3. epc笔记

    http://wenku.baidu.com/view/5e921520dd36a32d7375812a.html 1.  先注册, EPC注册EPS业务或non-EPS服务 ?? HSS做什么? 2 ...

  4. Elasticsearch中的分词器比较及使用方法

    Elasticsearch 默认分词器和中分分词器之间的比较及使用方法 https://segmentfault.com/a/1190000012553894 介绍:ElasticSearch 是一个 ...

  5. BZOJ3522 POI2014HOT-Hotels(树形dp)

    分两种情况.三点两两lca相同:在三点的lca处对其统计即可,显然其离lca距离应相同:某点在另两点lca的子树外部:对每个点统计出与其距离x的点有多少个即可. 可以长链剖分做到线性,当然不会. #i ...

  6. BZOJ3243 [Noi2013]向量内积 【乱搞】

    题目链接 BZOJ3243 题解 模数只有\(2\)或\(3\),可以大力讨论 如果模数为\(2\),乘积结果只有\(1\)或\(0\) 如果一个向量和前面所有向量乘积都为\(1\),那么其和前面向量 ...

  7. string 类型转换

    string转int "; int n = atoi(str.c_str()); cout << n << endl; int转string #include < ...

  8. NodeJS + PhantomJS 前端自动化资源监控

    前言:最近做前端资源监控,看了很多例子,没有达到想要的效果.首先的槽点是PhantomJS的官方文档,真鸡肋,其次是网上的例子,多数是介绍PhantomJS的用法,而并没有介绍怎么完整的去实现,跟官方 ...

  9. 给DOM元素绑定click事件也有学问

    最简单的莫过于使用click方法: 1 <input id="btn" type="button" value="BUTTON" on ...

  10. Codeforces ----- Kefa and Dishes [状压dp]

    题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...