Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在ASP.NETMVC中将用户登录尝试记录到文本文件中_Asp.net Mvc_Database_Logging_Access Control_Writetofile - Fatal编程技术网

Asp.net mvc 在ASP.NETMVC中将用户登录尝试记录到文本文件中

Asp.net mvc 在ASP.NETMVC中将用户登录尝试记录到文本文件中,asp.net-mvc,database,logging,access-control,writetofile,Asp.net Mvc,Database,Logging,Access Control,Writetofile,为了记录用户登录我的web应用程序的尝试,我需要将每次尝试保存到文本文件中。我创建了一个记录器模型,如下所示: public class Logger { public string name { get; set; } public string date { get; set; } public string ipAddress{ get; set; } } [HttpPost] [ValidateAntiForgeryToken]

为了记录用户登录我的web应用程序的尝试,我需要将每次尝试保存到文本文件中。我创建了一个记录器模型,如下所示:

public class Logger

 {
    public string name { get; set; }
    public string date { get; set; }
    public string ipAddress{ get; set; }
 }
     [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult Login(LoginModel model, string returnUrl)
     {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var loginResult = securityService.Login(model);

        if (!string.IsNullOrEmpty(loginResult.ErrorMessage))
        {
            ModelState.AddModelError(string.Empty, 
            loginResult.ErrorMessage);
            return View(model);
        }
        else
        {

            authManager.SignIn(claimsIdentity);

            HttpOnly = true
            });

            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            return RedirectToAction("Index", "Home");
        }
    }
以下是我需要为其创建记录器的登录模型:

 public class LoginModel

  {
     [Display(Name = "User Name")]
     public string Username{ get; set; }
     public string Password { get; set; }

  }
验证控制器中用户登录的方法如下:

public class Logger

 {
    public string name { get; set; }
    public string date { get; set; }
    public string ipAddress{ get; set; }
 }
     [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult Login(LoginModel model, string returnUrl)
     {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var loginResult = securityService.Login(model);

        if (!string.IsNullOrEmpty(loginResult.ErrorMessage))
        {
            ModelState.AddModelError(string.Empty, 
            loginResult.ErrorMessage);
            return View(model);
        }
        else
        {

            authManager.SignIn(claimsIdentity);

            HttpOnly = true
            });

            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            return RedirectToAction("Index", "Home");
        }
    }
我想重用上面的方法来写入文本文件,以存储输入视图的用户名等信息。
有什么建议吗?

我建议您使用。我在我的项目中也使用了同样的方法,效果非常好。您需要为它创建一个对象,并且可以直接使用它。以下是它的小样本:

Logger logger = LogManager.GetCurrentClassLogger();
logger.Info("Error while exporting QE reportlet from Dashboard -" + ex.Message + ex.StackTrace);
您需要安装两个包Nlog和Nlog.config。安装这些之后,您将在项目中看到Nlog.config。在这种情况下,您将找到一个目标和规则标记,您需要对其进行如下修改:

<targets>
<target
  name="logfile"
  xsi:type="File"
  fileName="D:\logging\mylogs.log"
  archiveFileName="D:\logging\mylogs.{#}.log"
  archiveNumbering="Date"
  archiveEvery="Day"
  archiveDateFormat="yyyyMMdd"
/>
 </targets>
<rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
  </rules>


建议:使用日志框架。不要问哪个,堆栈溢出不是问这些问题的地方。谷歌是你的朋友。我确实研究了一些日志框架,挑战是没有那么多的例子来演示ASP.NETMVC5,大多数例子都是ASP.NETCore。