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

可是總感受 投機取巧的使用了web端依賴注入的功能;json

各位請吐槽。。有什麼高見儘管留言,看到後一一回復app

相關文章
相關標籤/搜索