投稿

ラベル(NLog)が付いた投稿を表示しています

c# .netframework WebAPIで呼び出し元をNLogする

APIで呼び出し元を出力する必要があったのでメモしておく。 System.Collectionsは、nugetでインストールする必要があるかもしれない。 dynamicはstringでキャストしないとExceptionを吐く。 using System.Collections.Generic; [RoutePrefix("api")] public class ApiTestController : ApiController { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private string httpContext = "MS_HttpContext"; /// /// ログイン /// /// For Debug [HttpGet] [Route(nameof(Index))] public Dictionary<string, string> Index() { logger.Info("=== Index Controller START"); dynamic ctx = this.Request.Properties[httpContext]; if (ctx != null) { logger.Info("UserHostAddress: {0}", (string)ctx.Request.UserHostAddress); logger.Info("UrlReferrer {0}", (string)ctx.Request.UrlReferrer); Dictionary<string, string> dict = new ...

web.configにNlog設定を踏襲し、変換構文を使ってソリューション構成ごとに異なるログ設定をする

アクション 最初に以下を宣言する。 これで、ソースコードにはログを貼るだけでよくなる。   <configSections>     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>   </configSections> あとは、targetとruleを付けるだけ。 全体像は以下の通り。 Web.config <configuration>   <configSections>     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>   </configSections>   <nlog>     <targets>       <target name="logFile" type="File" fileName="./logs/${shortdate}-debug.log" encoding="UTF-8"         archiveFileName="${basedir}/logs/archives/archive.${shortdate}-debug.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="365"         layout="${longdate},${uppercase:${level}},${callsite},${callsite-linenumber},${message}"/>       <!--       <target name=...

Nlog.configをDebug / Rlease構成で使い分ける

イメージ
ビルド時のソリューション構成で利用するNlog.configを使い分けたい。 最終的に作った構成をメモしておく。 (環境) IDE: Visual Studio Community 2019 Framework: .NetFramework4.5 本当は、 ${buildConfiguration}を使いたいが Nlog に不具合があり、まだ治っていない。 ※(参照: Filter by Build Configuration #1929 ) よって、プリプロセッサを使ってコードに埋め込む。 プロジェクト構成は以下の通り。 NLog.Debug.config NLog.Release.config を使う。 ※NLog.configは存在しているが使わない。 Global.asax.csにNlog.configの読み込みコードを埋め込む。 Global.asax.cs using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace WebMVC {     public class MvcApplication : HttpApplication     {         protected void Application_Start()         {             AreaRegistration.RegisterAllAreas();             FilterConfig.RegisterGlobalFilt...

NLogでLog Parser Studioを使う

イメージ
準備することは以下の通り。 1. ログフォーマットをCSVにする 2. 出力ファイルにヘッダを付けておく 1. ログフォーマットをCSVにする NLogで作成されたログファイルをLog Parser Studio(LPS)で読み込むために、ファイルはCSV形式にしておく。 Nlog.configは以下の通り。layout部分の設定がCSV形式となる。 <targets>     <target xsi:type="File" name="logfile" fileName="./logs/${shortdate}.log"         encoding="UTF-8"         archiveFileName="${basedir}/logs/archives/archive.{#}.log"         archiveEvery="Day"         archiveNumbering="Rolling"         maxArchiveFiles="7"         layout="${longdate},${uppercase:${level}},${callsite},${callsite-linenumber},${message}" /> 2. 出力ファイルにヘッダを付けておく $(shortdate).logを作成する。(ex. 2020-07-23.log)を作成し、ヘッダに 「date, level, class, line, message」とか記載しておく。 プログラムを実行すると、ログは以下の通りとなる。 2020-07-23 17:44:41.2477,DEBUG,WebMVC.Controllers.HomeController.Index,17,d...

.NetFrameworkでNlog(Nlog.config)を使う

.NetFramework4.5でNlogを使ってみた。 サンプルアプリは、 CreateASP.NET MVC Application を作った。 IDEはVisual Studio2019 手順は以下の通り。 1. NugetパッケージマネージャーでNlogをインストールする 2. NLog.configを設定する 3. ターゲットClassにコーディングする 1. NugetパッケージマネージャーでNlogをインストールする インストールするパッケージは以下の通り。 NLog NLog.Config NLog.Schema(NLog.Configと同時にインストールされる) 2. NLog.configを設定する NLog.configは以下の通り <?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"       xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"       autoReload="true"       throwExceptions="false"       internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">     <targets>         <target xsi:type="File" name="logfile" fileName="./logs/${shortdate}.log"  ...