简体   繁体   English

如何使用MVC中的Nlog将消息记录到txt文件?

[英]How can I log a messages to a txt file with Nlog in MVC?

I am using Nlog in my MVC project 我在我的MVC项目中使用Nlog

What I want to achieve is to log a message when a action method is executed in my controller. 我想要实现的是在我的控制器中执行操作方法时记录消息。 and the log message should appear on a txt file. 并且日志消息应出现在txt文件中。 But I cant seem to get it to work 但我似乎无法让它发挥作用

I have installed Nlog to my references. 我已将Nlog安装到我的参考资料中。

I've got Nlog.Config with following configuration: 我有Nlog.Config,配置如下:

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <target name="file"
                xsi:type="File"
                layout="${longdate} ${logger} ${message}"
                fileName="${basedir}/logs/logfile.txt"
                keepFileOpen="false"
                encoding="iso-8859-2" />
    </targets>
    <rules>
        <logger name="*"
                minlevel="Debug"
                writeTo="file" />
    </rules>
</nlog>

and then inside my POST action method in my controller I have following code: 然后在我的控制器中的POST动作方法中,我有以下代码:

[HttpPost]
public ActionResult Edit(int id, CreateNKIphase1ViewModel model)
{
    Logger logger = LogManager.GetCurrentClassLogger();
    logger.Info("Test message");

     //code....
}

Where can I find the txt file? 我在哪里可以找到txt文件?

Why is this not working :S 为什么这不起作用:S

If your application is running in IIS using an application pool identity, you will need to grant folder permissions to that identity. 如果您的应用程序使用应用程序池标识在IIS中运行,则需要为该标识授予文件夹权限。

  • Create the 'logs' folder. 创建“logs”文件夹。
  • In IIS Manager, find the app pool identity that your application is using. 在IIS管理器中,找到应用程序正在使用的应用程序池标识。
  • In the 'logs' folder property dialog, grant write permission to the app pool identity. 在“logs”文件夹属性对话框中,为应用程序池标识授予写入权限。

When you enter the name for the app pool identity, you will need to include IIS APPPOOL . 输入应用程序池标识的名称时,需要包含IIS APPPOOL For example, if the app pool identity is ASP.NET v4.0 , enter the name as IIS APPPOOL\\ASP.NET v4.0 . 例如,如果应用程序池标识是ASP.NET v4.0 ,请输入名称为IIS APPPOOL\\ASP.NET v4.0 Also, make sure the location is the local machine, not a network domain. 此外,请确保该位置是本地计算机,而不是网络域。

Why not set the fileName attribute to a known path and name, rather than using the ${basedir} format? 为什么不将fileName属性设置为已知路径和名称,而不是使用${basedir}格式?

fileName="C:\Temp\logfile.txt"

That way you will know where the file is supposed to be. 这样你就会知道文件应该在哪里。 Once you have that part of the logging down, you can start adding in the more advanced techniques. 一旦您记录了该部分记录,您就可以开始添加更高级的技术。

It's perfectly working, but ${basedir} for asp.net application isn't same as for typical desktop application. 它完美地工作,但asp.net应用程序的$ {basedir}与典型的桌面应用程序不同。 I believe this file is available from temporary folders, not far from your application assemblies. 我相信这个文件可以从临时文件夹中获得,距离应用程序集不远。

You either can define your own variable or define a direct access to the 'logs' folder. 您可以定义自己的变量,也可以定义对“logs”文件夹的直接访问。 Make sure that application can write logs into this folder. 确保应用程序可以将日志写入此文件夹。 Use this answer, it's perfectly describe what you should do 使用这个答案,它完美地描述了你应该做的事情

如果在调试模式下运行应用程序,可以通过以下路径找到它:

"%PARTITION%:\\...PATH TO YOUR APP...\bin\Debug\\...

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

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