简体   繁体   English

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

[英]How log the client IP address with NLog in ASP.NET MVC

I made a portal with ASP.NET MVC and I need to log the IP address of any client that opens this website.我使用 ASP.NET MVC 创建了一个门户,我需要记录打开此网站的任何客户端的 IP 地址。 I added NLog with NuGet to my project and I added below tags in Web.config :我将带有 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>

how set my application to log any other info in addition to IP address of any computer that registers data with my portal?除了向我的门户注册数据的任何计算机的IP 地址之外,如何设置我的应用程序以记录任何其他信息? Any help will be apprecited!任何帮助将不胜感激!

Layout on the target configuration can be modified to add the machine name and the Client IP address可以修改目标配置上的布局以添加机器名称和客户端 IP 地址

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

More on this you can find here https://github.com/NLog/NLog/wiki/AspNet-Request-IP-Layout-Renderer有关此的更多信息,您可以在此处找到https://github.com/NLog/NLog/wiki/AspNet-Request-IP-Layout-Renderer

Edit: You may need NLog.Web for ASP.NET Web API and MVC projects.编辑:您可能需要 NLog.Web 用于 ASP.NET Web ZDB974238714CA8DE634A7MCCE1F 项目。 Also note that you may get::1 as the IP address in the localhost but it works fine when hosted.另请注意,您可能会在本地主机中获得::1 作为 IP 地址,但在托管时它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM