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用起来最舒服.不过现在的工作环境里使用它的机会比较少,大部分时候只是用来写 ...
随机推荐
- BZOJ 4031 HEOI2015 小Z的房间 基尔霍夫矩阵+行列式+高斯消元 (附带行列式小结)
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可 ...
- Daily Scrum02 11.29
今天大家都已经开始了进行第二轮迭代的工作!相比第一轮迭代,每个人都已经有了一定开发经验,这次做起来顺手很多.薛神和肖犇的挑战最大,他们需要实现好友功能,手机间的通信.服务器的搭建都是难点,但他们的热情 ...
- [转]Hexo博客添加访问统计 - 记录
引入不蒜子 <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"> ...
- lintcode-51-上一个排列
51-上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 注意事项 排列中可能包含重复的整数 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其 ...
- 基于网络的 Red Hat 无人值守安装
基于网络的 Red Hat 无人值守安装 本文介绍了 PC 平台上的一种快速 Red Hat Linux 安装方案.它具有很高的自动化程度--用户只需手工启动机器并选择从网络启动,就可以完成整个安装过 ...
- [剑指Offer] 24.二叉树中和为某一值的路径
[思路] ·递归先序遍历树, 把结点加入路径. ·若该结点是叶子结点则比较当前路径和是否等于期待和. ·弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点 注:路径定义为从树的根结点开始往 ...
- LeetCode--Factorial Trailing Zeroes(注意)
Given an integer n, return the number of trailing zeroes in n!. 问题描述:给出一个正整数n,计算n!结构后面有几个0.要求:在多项式时间 ...
- How to Create a Perl Based Custom Monitor on NetScaler
How to Create a Perl Based Custom Monitor on NetScaler https://support.citrix.com/article/CTX227727 ...
- BZOJ1305 [CQOI2009]dance跳舞 【网络流】
1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 3714 Solved: 1572 [Submit][ ...
- poj 1201 TYVJ 1415 Intervals
Description: 给定n个闭区间[ai,bi] 和n个整数ci,你需要构造一个集合Z,使得对于任何的i∈[1,n],Z中满足x∈[ai,bi]的x不少于ci个 求这样的整数集合Z至少包含多少个 ...