Net Core 控制台程序使用Nlog 输出到log文件
using CoreImportDataApp.Common;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using CoreImportDataApp.Services;
using NLog;//NLog.Extensions.Logging 和NLog.Web.AspNetCore两个类库。
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Hosting; namespace CoreImportDataApp
{
class Program
{
public static string SqlConnecting { get; set; }
static void Main(string[] args)
{
/**
* 在ASP.NET Core中使用依赖注入中使用很简单,只需在Startup类的ConfigureServices()方法中,
* 通过IServiceCollection接口进行注入即可,其它的无需关心
*
* 在控制台程序中就不一样了,除了注入外,你还需要构建容器,解析注入。
* 注入通过IServiceCollection接口,而构建容器需要调用IServiceCollection的扩展方法BuildServiceProvider(),
* 解析需要调用IServiceProvider的扩展方法GetService<T>()
**/
var builder = new ConfigurationBuilder()
.AddJsonFile("appSetting.json");
var configuration = builder.Build(); SqlConnecting = configuration.GetConnectionString("DefaultConnection"); IServiceCollection services = new ServiceCollection();
services.AddOptions();
services.Configure<TableStoreModel>(configuration.GetSection("TableStore"));
services.AddSingleton<TableStoreModel>();
services.AddTransient<ILoggerFactory, LoggerFactory>();
services.AddTransient<ITest, Test>(); IServiceProvider serviceProvider = services.BuildServiceProvider(); TestDI testDI = new TestDI(serviceProvider);
testDI.StartWork();
var host = new WebHostBuilder().UseKestrel().UseStartup<Startup>().Build();
host.Run();
Console.ReadLine();
}
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Web; namespace CoreImportDataApp
{
public class Startup
{
public static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");
app.Run(context=>
{
return context.Response.WriteAsync("bido-Nlog");
});
}
}
}
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using NLog;
using NLog.Extensions.Logging; namespace CoreImportDataApp.Services
{
public class Test:ITest
{
public string Add()
{
Startup.log.Info("运行中....");
return "ITest => test /add()";
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt"> <!-- define various log targets -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="D:\ProjectCode\C#Test\NetCore\netcoreWebApi\BidoCoreApi\CoreImportDataApp\bin\logs\nlog-all\${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="ownFile-web" fileName="D:\ProjectCode\C#Test\NetCore\netcoreWebApi\BidoCoreApi\CoreImportDataApp\bin\logs\nlog-own\${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}" /> <target xsi:type="Null" name="blackhole" />
</targets> <rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Warn" writeTo="allfile" /> <!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Debug" writeTo="ownFile-web" />
<!--Trace Debug Info Warn ERROR Fatal-->
</rules>
</nlog>
以上是在网上综合找到的结果;
但是总感觉 投机取巧的使用了web端依赖注入的功能;
各位请吐槽。。有什么高见尽管留言,看到后一一回复
Net Core 控制台程序使用Nlog 输出到log文件的更多相关文章
- 如何在.NET Core控制台程序中使用依赖注入
背景介绍 依赖注入(Dependency Injection), 是面向对象编程中的一种设计原则,可以用来减低代码之间的耦合度.在.NET Core MVC中 我们可以在Startup.cs文件的Co ...
- 在.NET Core控制台程序中使用依赖注入
之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...
- .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 ...
- NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
.NET Core 控制台程序没有 ASP.NET Core 的 IWebHostBuilder 与 Startup.cs ,那要读 appsettings.json.注依赖.配日志.设 IOptio ...
- VisualStudioCode创建的asp.net core控制台程序部署到linux
1.asp.net core控制台程序 static void Main(string[] args) { ; ) { Console.WriteLine("Hello World!&quo ...
- 大比速: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提供了众多的远程服务形式.在只 ...
- 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像
Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...
- Supervisor守护DotNet Core控制台程序
Supervisor 相信对Linux系统很熟的都知道这个软件,基于Python写的一个守护进程软件.具体的介绍和使用我就不再赘述了. 使用asp.net core 部署在Linux常用的方法 我们可 ...
- Mac/Windows开发跨平台.NET Core 控制台程序
自从微软开始在Github上开源搞.NET Core后,.NET的跨平台逐渐就成真了.多年使用各种语言,说实话还是csharp用起来最舒服.不过现在的工作环境里使用它的机会比较少,大部分时候只是用来写 ...
随机推荐
- gitbook.explore更新升级了, 不能再搜索了
www.gitbook.com/explore 不再是一个索引页面 Can I browse existing projects on GitBook ? The new version of Git ...
- 功能规格说明书Version2
此功能规格说明书是Week8 功能规格说明书的第二个版本, 版本1地址:http://www.cnblogs.com/Z-XML/p/3407687.html 此功能规格说明书是面向用户的,所以作者将 ...
- PokeCats开发者日志(六)
现在是PokeCats游戏开发的第九天的晚上,终于将这玩意提交到360移动开放平台进行审核了. 貌似很多平台都需要看这个著作权证明,得了,那我就话400块钱走一遍流程玩玩吧! 办理著作权还 ...
- Resources for Browser/Webkit/GPU
Tali Garsiel's site: http://taligarsiel.com/ Webkit Main Flow Chromium Project: http://www.chromium. ...
- struts2 下载文件
作者:禅楼望月 当下载的文件名字中不含有汉字,或者下载的文件不需要考虑用户的权限问题时.直接让超链接的href属性为所要下载的文件名即可.否则最好使用struts2的文件下载机制. 以下载图片为例 完 ...
- JavaScript归并方法reduce()和reduceRight()
ECMAScript 5还新增了两个归并数组的方法:reduce()和reduceRight().这两个方法都会迭代数组的所有项,然后构建一个最终返回的值.其中,reduce()方法从数组的第一项开始 ...
- LeetCode -- Linked List Circle ii
Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- [codeforces] 633C Spy Syndrome 2
原题 Trie树+dp 首先,我们可以简单的想到一种dp方式,就是如果这一段可以匹配并且可以与前一段接上,那么更新dp[i]为当前字符串的编号,然后倒推就可以得到答案. 但是,显然我们不能O(m)比较 ...
- Angular Cookie 读写
var app = angular.module('Mywind',['ui.router']) app.controller('Myautumn',function($scope,$http,$fi ...
- codeforces902C. Hashing Trees
https://codeforces.com/contest/902/problem/C 题意: 给你树的深度和树的每个节点的深度,问你是否有重构,如果有重构输出两个不同的结构 题解: 如果相邻节点的 ...