{"id":1089,"date":"2019-05-04T09:12:01","date_gmt":"2019-05-04T01:12:01","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1089"},"modified":"2022-02-17T15:29:26","modified_gmt":"2022-02-17T07:29:26","slug":"dot-net-core-log","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2019\/05\/04\/dot-net-core-log\/","title":{"rendered":"Dot Net Core Log \u900f\u904e\u76f8\u4f9d\u6ce8\u5165 NLog"},"content":{"rendered":"<p>\u76f8\u4fe1\u5927\u5bb6\u5c0d\u65bc Log \u7684\u91cd\u8981\u6027\u90fd\u6709\u4e00\u5b9a\u7684\u8a8d\u77e5\uff0c\u672c\u7bc7\u7b46\u8005\u5206\u4eab\u4e00\u4e0b\u5982\u4f55\u900f\u904e\u6ce8\u5165 <a href=\"https:\/\/nlog-project.org\/\" title=\"NLog\" rel=\"nofollow noopener\" target=\"_blank\">NLog<\/a> \u4e26\u4e14\u900f\u904e <a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/logging\/?view=aspnetcore-2.2\" title=\"Mircosoft.Extensions.Logging\" rel=\"nofollow noopener\" target=\"_blank\">Mircosoft.Extensions.Logging<\/a> \u4f86\u89e3\u9664\u5c0d\u65bc <a href=\"https:\/\/nlog-project.org\/\" title=\"NLog\" rel=\"nofollow noopener\" target=\"_blank\">NLog<\/a> \u7684\u76f8\u4f9d\uff0c\u85c9\u6b64\u5be6\u4f5c Dot Net Core Log \u6a5f\u5236\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5927\u5bb6\u5728\u4f7f\u7528Logger\u7d00\u9304Log\u6642\u5e38\u6703\u6709\u500b\u554f\u984c\uff0c\u8981\u7528NLog\u9084\u662flog4net\uff0c\u518d\u4f86\u5c31\u662f\u5be6\u9ad4\u8981\u600e\u9ebc\u751f\u6210\uff0c\u8981\u7528Singleton\u9084\u662f\u8981\u7528static\u51fd\u5f0f\uff0c\u6700\u5f8c\u8981\u600e\u9ebc\u7d71\u4e00Log\u7684\u683c\u5f0f\uff0c\u4eca\u5929\u7b46\u8005\u4f86\u5206\u4eab\u6211\u7684\u4f5c\u6cd5\uff1a<\/p>\n<ul>\n<li>\u5229\u7528 Mircosoft.Extensions.Logging \u4f86\u5c07Logger\u62bd\u8c61\u5316\uff0c\u5f9e\u6b64\u4e0d\u7528\u5728\u5be6\u505a\u6642\u64d4\u5fc3\u8981\u7528NLog\u6216\u662flog4net\u3002<\/li>\n<li>\u5229\u7528\u76f8\u4f9d\u6ce8\u5165\uff0c\u5be6\u4f5c\u53ea\u9700\u8981\u5728\u5efa\u69cb\u5b50\u8aaa\u660e\u9700\u8981\u4ec0\u9ebc\uff0c\u4e0d\u9700\u8981\u8003\u616e\u9700\u8981\u5143\u4ef6\u7684\u751f\u6210\u8207\u6d88\u6ec5\u3002<\/li>\n<li>\u5b78\u7fd2 NLog \u7684\u8a2d\u5b9a\u6a94\u8a2d\u5b9a\u65b9\u5f0f\u4ee5\u53ca\u81ea\u5b9a\u7fa9\u64f4\u5145\u65b9\u6cd5\u4f86\u683c\u5f0f\u5316Log\u7684\u5167\u5bb9\u3002<\/li>\n<\/ul>\n<h2>\u76f8\u4f9d\u5957\u4ef6\u4ee5\u53ca\u74b0\u5883\u8a2d\u5b9a<\/h2>\n<p>\u6b64\u7bc7\u6587\u7ae0\u662f\u57fa\u65bc.Net Core\u7684\u7248\u672c\u5beb\u6210\u7684\uff0c\u4ee5\u4e0b\u5217\u51fa\u6240\u9700\u5957\u4ef6\uff1a<\/p>\n<ul>\n<li>NLog.Config<\/li>\n<li>NLog.Web.AspNetCore<\/li>\n<\/ul>\n<p>\u7531\u65bc\u5b89\u88dd NLog.Web.AspNetCore \u5c31\u6703\u5b89\u88dd Mircosoft.Extensions.Logging \u6240\u4ee5\u4e0d\u5c07\u5176\u5217\u5728\u76f8\u4f9d\u5957\u4ef6\uff0c\u63a5\u4e0b\u4f86\u5c31\u4e00\u6b65\u4e00\u6b65\u8aaa\u660e\u3002<\/p>\n<h2>\u5be6\u4f5c\u65b9\u5f0f<\/h2>\n<h3>Mircosoft.Extensions.Logging<\/h3>\n<p>\u6211\u5011\u4f7f\u7528 Mircosoft.Extensions.Logging \u662f\u8981\u5c07Logger \u62bd\u8c61\u5316\uff0c\u4e5f\u5c31\u662f\u4f7f\u7528 Logger \u7684\u7269\u4ef6\u76f8\u4f9d\u7684\u662f Mircosoft.Extensions.Logging \u7684 <code>ILogger<\/code> \u800c\u4e0d\u662f NLog \u7684\u3002\u9019\u6a23\u505a\u4e4b\u5f8c\u5982\u679c\u8981\u66f4\u63db Logger \u7684\u5be6\u9ad4\u5c31\u4e0d\u6703\u5f71\u97ff\u5230\u7a0b\u5f0f\u78bc\u672c\u8eab\u4e86\u3002<br \/>\n\u8aaa\u4e86\u9019\u9ebc\u591a\u63a5\u4e0b\u4f86\u5c31\u770b\u770b\u7b46\u8005\u600e\u9ebc\u5be6\u4f5c\uff0c\u9996\u5148\u5047\u8a2d\u6211\u5011\u8981\u5be6\u4f5c\u4e00\u500b HomeController \u6211\u5011\u53ea\u8981\u900f\u904e\u5efa\u69cb\u5b50\u5c07 <code>ILogger<\/code> \u6ce8\u5165\u5373\u53ef\uff0c\u8acb\u770b\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-csharp\">public class HomeController : Controller\n{\n    private readonly ILogger _logger;\n\n    public LoggerService(ILogger&lt;HomeController&gt; logger)\n    {\n        _logger = logger;\n    }\n\n    public IActionResult Index () {\n        _logger.LogInformation(&quot;This is Index Log&quot;);\n        return View ();\n    }\n}<\/code><\/pre>\n<p>\u5982\u6b64\u7576\u4f7f\u7528\u8005\u9020\u8a2a HOME\\Index\\ \u6642\u4ed6\u5c31\u6703\u4f7f\u7528\u6ce8\u5165\u7684 logger \u7684 LogInformation \u7684\u65b9\u6cd5\u4e86\uff0c\u6b64\u6642\u4e5f\u4e0d\u7528\u5728\u4e4e\u5176\u4f7f\u7528\u7684\u662f NLog \u9084\u662f log4net \u4e86\uff0c\u9019\u5c31\u770b\u6ce8\u5165\u7684 Logger \u662f\u54ea\u4e00\u500b\u4e86\u3002<\/p>\n<h3>\u6ce8\u5165<\/h3>\n<p>\u7576\u4f7f\u7528 .Net Core \u6ce8\u5165\u5c31\u8b8a\u5f97\u5bb9\u6613\u8a31\u591a\u4e86\uff0c\u9019\u88e1\u53ea\u9700\u8981\u5728 <code><code>Program.cs<\/code><\/code> \u52a0\u5165\u4e00\u6bb5\u7a0b\u5f0f\u78bc\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-csharp\">using NLog.Web; \/\/ &lt;--\u8a18\u5f97Using\u9019\u500b\u4e0d\u7136C#\u4e0d\u8a8d\u8b58\u8981\u65b0\u589e\u7684\u65b9\u6cd5\uff0e\n        .\n        .\n        .\n        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;\n            WebHost.CreateDefaultBuilder(args)\n                .UseStartup&lt;Startup&gt;()\n                .UseNLog(); \/\/ &lt;--\u591a\u52a0\u9019\u884c\u5373\u53ef<\/code><\/pre>\n<p>\u7531\u65bc\u5f15\u7528 NLog.Web \u6703\u6709\u64f4\u5145\u65b9\u6cd5 UseNLog\u8b93\u6ce8\u5165\u5728\u4e00\u884c\u5167\u5b8c\u6210\u3002<\/p>\n<h3>\u683c\u5f0f\u5316 Log \u5167\u5bb9<\/h3>\n<p>\u4e5f\u8a31\u4f7f\u7528\u8005\u6703\u89ba\u5f97 LogInformation \u9019\u65b9\u6cd5\u540d\u7a31\u592a\u9577\uff0c\u60f3\u8981\u6539\u77ed\u4e00\u9ede\uff0c\u53c8\u6216\u8005\u60f3\u8981\u7d00\u9304\u65b9\u6cd5\u540d\u7a31\u8ddf\u884c\u865f\uff0c\u9019\u6642\u6211\u5011\u4f86\u81ea\u5b9a\u64f4\u5145\u65b9\u6cd5\uff0c\u900f\u904e\u64f4\u5145\u65b9\u6cd5\u4f86\u683c\u5f0f\u5316 Log \u7684\u5167\u5bb9\uff0c\u4ee5\u53ca\u4f7f\u7528\u81ea\u8a02\u7fa9\u7684\u65b9\u6cd5\u540d\u7a31\uff0c\u9019\u88e1\u6211\u5011\u52a0\u5165 ILogger.Extension.cs \u4e26\u52a0\u5165\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code class=\"language-csharp\">using System.Runtime.CompilerServices;\nnamespace ILogger.Extension\n{\n    public static class ILoggerExtensions\n    {\n        public static void Info(this ILogger logger,\n            string log,\n            [CallerMemberName] string name = &quot;&quot;,\n            [CallerLineNumber] int line = 0)\n        {\n            logger.LogInformation($&quot;Method:{name}, Line:{line}, Message:{log}&quot;);\n        }\n    }   \n}<\/code><\/pre>\n<p>\u9019\u88e1\u6211\u5011\u4f7f\u7528 <a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/programming-guide\/concepts\/caller-information\" title=\"\u547c\u53eb\u8cc7\u8a0a\" rel=\"nofollow noopener\" target=\"_blank\">\u547c\u53eb\u8cc7\u8a0a<\/a> \u4f86\u5217\u51fa\u65b9\u6cd5\u540d\u7a31\u53ca\u884c\u865f\uff0c\u4e26\u5b9a\u7fa9\u6bcf\u500b Log \u90fd\u5305\u542b\u9019\u8cc7\u8a0a\u3002<br \/>\n\u63a5\u4e0b\u4f86\u5728\u4f7f\u7528\u5230 ILogger \u7684\u5730\u65b9\u591a using \u9019\u500b namespace \u5c31\u53ef\u4ee5\u4f7f\u7528\u9019\u500b\u64f4\u5145\u65b9\u6cd5\u4e86\u3002<\/p>\n<p>\u7576\u7136 Log \u7684\u5c64\u7d1a\u4e0d\u53ea\u662f Info \u800c\u5df2\uff0c\u63a5\u4e0b\u4f86\u518d\u8acb\u4f7f\u7528\u8005\u4f9d\u6a23\u756b\u846b\u8606\u5be6\u4f5c\u5176\u4ed6\u5c64\u7d1a\u5427\u3002<\/p>\n<h3>NLog Config \u8a2d\u5b9a<\/h3>\n<p>\u7576\u5b89\u88dd NLog.Config \u5b89\u88dd\u7a0b\u5f0f\u6703\u5728\u57f7\u884c\u6a94\u6240\u5728\u76ee\u9304\u7522\u751f NLog.config \u6a94\u6848\uff0c\u63a5\u4e0b\u4f86\u7c21\u55ae\u4fee\u6539\u5c31\u53ef\u4ee5\u628a Log \u5beb\u5230\u6a94\u6848\u5167\uff0c\u4ee5\u4e0b\u7bc4\u4f8b\u771f\u5c0d\u8a2d\u5b9a\u5167\u5bb9\u505a\u4e00\u7c21\u55ae\u7684\u8aaa\u660e\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;\n&lt;nlog xmlns=&quot;http:\/\/www.nlog-project.org\/schemas\/NLog.xsd&quot;\n      xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n      xsi:schemaLocation=&quot;http:\/\/www.nlog-project.org\/schemas\/NLog.xsd NLog.xsd&quot;\n      autoReload=&quot;true&quot;\n      throwExceptions=&quot;false&quot;\n      internalLogLevel=&quot;Off&quot; internalLogFile=&quot;c:\\temp\\nlog-internal.log&quot;&gt;\n\n  &lt;targets&gt;\n\n        &lt;target xsi:type=&quot;File&quot; name=&quot;f&quot; fileName=&quot;${basedir}\/logs\/${shortdate}.log&quot;\n            layout=&quot;${longdate} ${uppercase:${level}} ${message}&quot; \/&gt;\n  &lt;\/targets&gt;\n\n  &lt;rules&gt;\n    &lt;!-- add your logging rules here --&gt;\n\n    &lt;logger name=&quot;Microsoft.*&quot; maxlevel=&quot;Info&quot; final=&quot;true&quot; \/&gt; &lt;!-- BlackHole without writeTo --&gt;\n    &lt;logger name=&quot;*&quot; minlevel=&quot;Debug&quot; writeTo=&quot;f&quot; \/&gt;\n  &lt;\/rules&gt;\n&lt;\/nlog&gt;\n<\/code><\/pre>\n<p>\u9019\u908a\u505a\u500b\u7c21\u55ae\u4ecb\u7d39\uff0c\u9996\u5148\u8a2d\u5b9a\u4e00\u4e0b <code>target<\/code> \u518d\u4f86\u8a2d\u5b9a <code>rules<\/code> \u628a\u5c0d\u61c9\u7684 LOG \u5beb\u5230 <code>target<\/code> \u5167\uff0c\u7c21\u55ae\u8b1b <code>target<\/code> \u662f\u8981\u5beb\u5230\u54ea\u88e1\uff0c <code>rules<\/code> \u5c31\u662f\u60f3\u8981\u7d00\u9304\u7684 Log \u689d\u4ef6\u3002\u66f4\u9032\u4e00\u6b65\u5167\u5bb9\u8acb\u898b <a href=\"https:\/\/github.com\/nlog\/nlog\/wiki\/Configuration-file\" title=\"Nlog Configuration\" rel=\"nofollow noopener\" target=\"_blank\">NlogConfiguration<\/a> \u3002<\/p>\n<h2>\u7d50\u8a9e<\/h2>\n<p>\u4eca\u5929\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u7b46\u8005\u65bc .Net Core \u5be6\u4f5cLogger \u6a5f\u5236\u7684\u65b9\u5f0f\uff0c\u85c9\u7531\u9019\u6a23\u7684\u67b6\u69cb\u53ef\u4ee5\u4efb\u610f\u62bd\u63db Logger \u7684\u5be6\u4f5c\uff0c\u800c\u4e0d\u6703\u5f71\u97ff\u7a0b\u5f0f\u7684\u5be6\u4f5c\uff0c\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u8b93\u5927\u5bb6\u6709\u6240\u6536\u7a6b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76f8\u4fe1\u5927\u5bb6\u5c0d\u65bc Log \u7684\u91cd\u8981\u6027\u90fd\u6709\u4e00\u5b9a\u7684\u8a8d\u77e5\uff0c\u672c\u7bc7\u7b46\u8005\u5206\u4eab\u4e00\u4e0b\u5982\u4f55\u900f\u904e\u6ce8\u5165 NLog \u4e26\u4e14\u900f\u904e Mircoso &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2019\/05\/04\/dot-net-core-log\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Dot Net Core Log \u900f\u904e\u76f8\u4f9d\u6ce8\u5165 NLog\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[71,92,75],"class_list":["post-1089","post","type-post","status-publish","format-standard","hentry","category-develop","tag-c","tag-dotnetcore","tag-nlog"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1089"}],"version-history":[{"count":49,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"predecessor-version":[{"id":3024,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions\/3024"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}