如何在 ASP.NET MVC 中使用 NLog 记录客户端 IP 地址
How log the client IP address with NLog in ASP.NET MVC
我用 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 地址 以外的任何其他信息?我们将不胜感激!
可以修改目标配置上的布局以添加机器名称和客户端 IP 地址
layout="Machine Name: ${machinename} - IP: ${aspnet-request-ip}
您可以在此处找到更多相关信息 https://github.com/NLog/NLog/wiki/AspNet-Request-IP-Layout-Renderer
编辑:对于 ASP.NET Web API 和 MVC 项目,您可能需要 NLog.Web。
另请注意,您可能会在本地主机中获得 ::1 作为 IP 地址,但在托管时它工作正常。
我用 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 地址 以外的任何其他信息?我们将不胜感激!
可以修改目标配置上的布局以添加机器名称和客户端 IP 地址
layout="Machine Name: ${machinename} - IP: ${aspnet-request-ip}
您可以在此处找到更多相关信息 https://github.com/NLog/NLog/wiki/AspNet-Request-IP-Layout-Renderer
编辑:对于 ASP.NET Web API 和 MVC 项目,您可能需要 NLog.Web。 另请注意,您可能会在本地主机中获得 ::1 作为 IP 地址,但在托管时它工作正常。