DotNet Core Console 程序使用NLog

参考:https://github.com/NLog/NLog/wiki/Tutorial

步骤:

1. 使用Nuget安装NLog.Extensions.Logging

Install-Package NLog.Extensions.Logging

2.编写代码(到这步运行代码,不报错,但是也不会有log输出,因为没有设置配置文件)

3. 编写配置文件

在项目下新增加NLog.config 文件,并设置其能copy到运行目录。将一下内容粘到里面,重新运行程序就可以看到输出到file.txt的log.

    <?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">

      <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
      </targets>

      <rules>
        <logger name="*" minlevel="Info" writeTo="logfile" />
      </rules>
</nlog>

4. 增加输出源,将Log输出到屏幕上,按照以下内容对配置文件做修改

    <?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">

      <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
        <target name="console" xsi:type="Console" />
      </targets>

      <rules>
        <logger name="*" minlevel="Info" writeTo="logfile" />
        <logger name="*" minlevel="Info" writeTo="console" />
      </rules>
</nlog>

5. 如果想发送邮件,可以安装NLog.MailKit

Install-Package NLog.MailKit

6.在配置文件中添加发送邮件的设置部分(参考https://github.com/nlog/NLog/wiki/Mail-target

    <?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"
          internalLogFile="d:\Nlog_log.txt" internalLogLevel="Error">
      <variable name="smtpServer" value="***"/>
      <variable name="smtpUserName" value="***"/>
      <variable name="smtpPassword" value="***"/>
      <variable name="from" value="***"/>
      <variable name="to" value="***"/>

      <targets>
        <target name="logfile" xsi:type="File" fileName="logs\${date:format=yyyyMMdd}_log.txt" layout="${date:format=yyyy-MM-dd HH\:mm\:ss} ${message}" />
        <target name="console" xsi:type="Console" layout="${date:format=yyyy-MM-dd HH\:mm\:ss} [${level}] ${message}"/>
        <target name="infoMail" xsi:type="Mail"
                  smtpServer="${smtpServer}"
                  smtpUserName="${smtpUserName}"
                  smtpPassword="${smtpPassword}"
                  from="${from}"
                  to="${to}"
                  subject="info log"
                  body="${message}"
                  html="true"/>
        <target name="errorMail" xsi:type="Mail"
                  smtpServer="${smtpServer}"
                  smtpUserName="${smtpUserName}"
                  smtpPassword="${smtpPassword}"
                  from="${from}"
                  to="${to}"
                  subject="error log"
                  body="${message}"/>
      </targets>

      <rules>
        <logger name="***" minlevel="Info" writeTo="infoMail" />
        <logger name="***" minlevel="Error" writeTo="logfile" />
        <logger name="Main" minlevel="Info" writeTo="console" />
        <logger name="Main" minlevel="Error" writeTo="errorMail" />
      </rules>

      <extensions>
        <add assembly="NLog.MailKit"/>
      </extensions>
</nlog>
时间: 2024-07-30 09:27:44

DotNet Core Console 程序使用NLog的相关文章

在树莓派上运行dotNet core console 程序

---恢复内容开始--- 微软的dotNet Core 已经发布很长时间了,现在最新的版本是2.0.2 发现大家对dotNet Core 在service & micro service上的文章不少.不过本地程序却少的可怜. 作为CS端坚守的dotneter  我觉得应该做点什么,让误入dotNet CS端的小盆友们不要再无助下去. 好了身家,三围,银行卡号已经交代清楚,下来我们进入正题,看看如何在armhf设备上run起来一个dotNet Core的Console Application. 首

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

Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new console dotnet restore 创建 Dockerfile 文件,参考https://github.com/dotnet/dotnet-docker/blob/master/samples/aspnetapp/Dockerfile vim /home/console/Dockerfile #

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

1.asp.net core控制台程序 static void Main(string[] args) { int times=10; while(times>=0) { Console.WriteLine("Hello World!"); times--; Thread.Sleep(1000); } } 2.发布 发布前,修改test2.csproj文件(项目名称为test2) <Project Sdk="Microsoft.NET.Sdk">

将app接口服务器改为dotnet core承载

昨天我的一个 app 的接口服务器挂掉了,国外的小鸡意外的翻车,连同程序和数据一起,猝不及防.我的服务端程序是 asp.net mvc ,小鸡是 256 M 的内存跑不了 windows 系统,装的 mono .服务器用的 jexus,但是还有一个 apache+php+mysql 的全家桶占用了 80 端口,所以这个接口是通过 apache 反向代理的. 这样一来本来环境就很复杂了,我 ubuntu 16.04 装 mono 下载了差不多700 mb 的数据,安装后体积更大,简直太不环保了,只

.Net Core应用程序跨平台应用研究-HelloArm(串口篇)

引言 为了验证采用dotnet core技术开发的物联网设备数据采集接入服务应用是否能在高性价比的linux嵌入式平台运行,针对dotnet core应用程序进行嵌入式linux环境的发布部署运行验证研究. 硬件环境 硬件系统经过对比筛选,选用了友善之臂出品的NanoPC-T3 Plus.该控制板为掌上型嵌入式Linux系统迷你PC,采用64位三星arm CPU,具有完善的硬件接口和驱动支持,大于1G的内存和8G以上的eMMC闪存,具有较高的性能价格比. NanoPC-T3 Plus是友善之臂专

使用 dotnet watch 开发 ASP.NET Core 应用程序

使用 dotnet watch 开发 ASP.NET Core 应用程序 原文:Developing ASP.NET Core applications using dotnet watch作者:Victor Hurdugaci翻译:谢炀(Kiler)校对:刘怡(AlexLEWIS).许登洋(Seay) 介绍 dotnet watch 是一个开发阶段在源文件发生变动的情况下使用 dotnet 命令的工具. 当代码发生变动的时候可以用来执行编译,运行测试,或者发布操作. 在本教程中,我们将使用一个

NLOG在Console程序中运行时不起作用

把Web程序中的NLog.dll, NLog.config, NLog.xsd拷到Console程序中,发现运行不起作用,看了网上的示例教程,如http://www.cnblogs.com/sorex/archive/2013/01/31/2887174.html,试了下,多了个packages.config文件,里面内容如下: <?xml version="1.0" encoding="utf-8"?> <packages> <pac

.NET跨平台之旅:探秘 dotnet run 如何运行 .NET Core 应用程序

自从用 dotnet run 成功运行第一个 "Hello world" .NET Core 应用程序后,一直有个好奇心:dotnet run 究竟是如何运行一个 .NET Core 应用程序的? 在 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 与 在Linux上以本地机器码运行 ASP.NET Core 站点 之后,这个好奇心被进一步激发,于是“探秘 dotnet run”顺理成章地成为.NET跨平台之旅的下一站. 首先我们了解一下 dotnet 命令是

ubuntu上部署windows开发的dotnet core程序

目标:完成windows上开发的dotnet core程序部署至linux服务器上(Ubuntu 14.04) windows上开发dotnet core很简单,安装好VS2017,建立相关类型的项目进行开发即可. Linux服务器中需要做以下几个工作: 1.安装dotnet core, 运行dotnet程序必备条件. 2.安装supervisor守护进程,可以帮助你自动启动站点. 3.安装nginx做代理 1. 安装dotnet core #依次执行下面的命令,安装的版本有问题的话,按照错误提