如何使用NLog在ASP.NET MVC中记录客户端IP地址

问题描述 投票:0回答:1

我使用ASP.NET MVC创建了门户,我需要记录任何打开此网站的客户端的IP地址。我将带有NuGet的NLog添加到我的项目中,并在Web.config

中添加了以下标签。
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="default" xsi:type="File" fileName="logs/app-log.txt" archiveFileName="logs/archives/app-log.{#}.txt" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="7" />
    </targets>
    <rules>
      <logger name="*" writeTo="default" />
    </rules>
  </nlog>

如何设置我的应用程序以记录除向门户网站注册数据的任何计算机的IP地址之外的其他信息?任何帮助将不胜感激!

c# asp.net-mvc nlog
1个回答
0
投票

可以修改目标配置上的布局以添加计算机名称和客户端IP地址

layout="Machine Name: ${machinename} - IP: ${aspnet-request-ip}

关于此内容的更多信息,请点击这里https://github.com/NLog/NLog/wiki/AspNet-Request-IP-Layout-Renderer

© www.soinside.com 2019 - 2024. All rights reserved.