简体   繁体   English

在ASP.NET MVC项目中使用Serilog

[英]Using Serilog in ASP.NET MVC project

Today at work I was asked to add some basic logging functionality in my existing ASP.NET MVC (not Core) project. 今天,我被要求在我现有的ASP.NET MVC(不是Core)项目中添加一些基本的日志记录功能。 I was told to use Serilog for this. 有人告诉我为此使用Serilog。

However, I failed to find any reasonable example of using Serilog in MVC, all links simply refer to Core. 但是,我找不到在MVC中使用Serilog的任何合理示例,所有链接仅引用Core。 I tried to do everything myself. 我试图自己做所有事情。 After installing all necessary packages (Serilog, Serilog.Sinks.File), I created a "Logs" folder and tried this: 安装所有必需的软件包(Serilog,Serilog.Sinks.File)之后,我创建了一个“ Logs”文件夹并尝试了以下操作:

var log = new LoggerConfiguration()
  .WriteTo.File("~/Logs/log.txt")
  .CreateLogger();

log.Information("Hello from Serilog!");

I expected to see a new file in the folder but there wasn't any. 我希望在文件夹中看到一个新文件,但没有任何文件。 I also tried using a simpler path "log.txt" and creating a file in this folder preemptively but it didn't help. 我还尝试使用更简单的路径“ log.txt”并抢先在此文件夹中创建文件,但这没有帮助。

Is it possible that Serilog is not supported for MVC at all? MVC是否完全不支持Serilog? If it is, what is a simple way to log something into a text file? 如果是的话,将某些内容记录到文本文件中的简单方法是什么?

I doubt Serilog understands the ~/ syntax in your path to your log file. 我怀疑Serilog可以理解日志文件路径中的~/语法。 ~/ refers to the virtual root of your site, but Serilog doesn't know anything about virtual roots or websites. ~/站点的虚拟根目录,但是Serilog对虚拟根目录或网站一无所知。 It's just a logging framework and expects a normal path. 它只是一个日志记录框架,需要正常路径。 So if you want to log to a file called log.txt in the Logs directory of your site, then you need to convert it to a proper path that Serilog can understand. 因此,如果要登录到站点的Logs目录中名为log.txt的文件,则需要将其转换为Serilog可以理解的正确路径。

The System.Web.Hosting.HostingEnvironment.MapPath method can translate virtual root relative paths. System.Web.Hosting.HostingEnvironment.MapPath方法可以转换虚拟根相对路径。 So ~/Logs/log.txt can become something like C:\\path-to-your-site\\Logs\\log.txt . 因此~/Logs/log.txt可以变成类似C:\\path-to-your-site\\Logs\\log.txt

Change to this: 更改为此:

var log = new LoggerConfiguration()
    .WriteTo.File(System.Web.Hosting.HostingEnvironment.MapPath("~/Logs/log.txt"))
    .CreateLogger();

log.Information("Hello from Serilog!");    

//note that importing the namespace will make it so you don't have to fully qualify the name.

Then Serilog should log to the location you're expecting, if the directory exists and it has write privileges there. 然后,如果该目录存在并且在那里具有写权限,则Serilog应该登录到您期望的位置。

File logging from a web application is going to get very messy. 从Web应用程序记录文件将变得非常混乱。 Unless your site gets hardly any usage, the logs are going to become unmanageable and useless. 除非您的站点几乎没有使用任何方法,否则日志将变得难以管理且无用。 I suggest you look at the other Serilog sinks, and find one that logs to a logging service such as Seq (from the makers of Serilog) or Stackify Retrace . 我建议您查看其他Serilog接收器,并找到一个记录到日志记录服务的记录,例如Seq (来自Serilog的制造商)或Stackify Retrace

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

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