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 ...