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文件的更多相关文章

  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. [leetcode-634-Find the Derangement of An Array]

    In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no ...

  2. Daily Scrum 11.01

    全队进展速度很快,11月伊始都完成了初步的工作.交由负责整合工作的毛宇开始调试整合. Member Today's task  Tomorrow's task 李孟 task 616 测试 (活动) ...

  3. 浅谈c语言和c++中struct的区别

    今天做二叉树的时候,发现利用结构体有点乱,不知道怎么回事,我之前知道c语言中声明一个结构体变量时需要通过 struct 结构体名 变量名,而在c++中,可以不要struct,由于可以利用typedef ...

  4. 【EasyNetQ】- 请求回复

    EasyNetQ还支持请求/响应消息传递模式.这使得实现客户端/服务器应用程序变得容易,客户端向服务器发出请求,然后服务器处理请求并返回响应.与传统的RPC机制不同,EasyNetQ请求/响应操作没有 ...

  5. P2574 XOR的艺术

    题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[l,r ...

  6. Java Integer比较

    今天看微信做了一个选择题,对Integer比较结果有点意外,题目如下: public static void main(String[] args) { Integer a = 1; Integer ...

  7. MySQL之数据库及表的修改和删除

    本文章来自实验楼的操作过程和其中相应地解释.(博客园不知道怎么回事,上传图片总是失败.) 一.对数据库修改 1)删除数据库的命令为:DROP DATABASE 数据名; 二.对表的修改 1)重命名一张 ...

  8. 【bzoj2038】[国家集训队2010]小Z的袜子 莫队

    莫队:就是一坨软软的有弹性的东西Duang~Duang~Duang~ 为了防止以左端点为第一关键字以右端点为第二关键字使右端点弹来弹去,所以让左端点所在块为关键字得到O(n1.5)的时间效率,至于分块 ...

  9. 用npm安装express时报proxy的错误的解决方法

    首先要说明一点:当使用npm install <module-name>时安装组件时,安装的目录是cmd的目录+node_modules+组件名 例子如下:假如你现在安装express这个 ...

  10. lwIP RAW_API

    lwIP RAW TCP/IP接口 作者: Adam Dunkels, Leon Woestenberg, Christiaan Simons lwIP为使用TCP/IP协议通信的应用程序编程提供了两 ...