简体   繁体   English

在SeriLog ASP.NET中显示用户电子邮件

[英]Display user email in SeriLog ASP.NET

I am logging data into SeriLog using .Net Cli. 我正在使用.Net Cli将数据记录到SeriLog中。 An issue arises when displaying a user's email. 显示用户的电子邮件时出现问题。 This is what I get: System.Collections.Generic.List`1[Net3.Models.AccountViewModels.UserListingVM], although I am trying to obtain: someonesname@email.com. 这是我得到的:System.Collections.Generic.List`1 [Net3.Models.AccountViewModels.UserListingVM],尽管我正在尝试获取:someonesname@email.com。 I am not sure why I'm not getting the results that i want, but here is the code below from the Controller and the Repository: PS I'm pretty new at this. 我不确定为什么我没有得到想要的结果,但是这是下面的Controller和Repository中的代码:PS在这方面我很新。 Thank you in advance. 先感谢您。

*****UserController.cs***** ***** UserController.cs *****

           using System;
           using System.Collections.Generic;
           using System.Linq;
           using System.Threading.Tasks;
           using Microsoft.AspNetCore.Http;
           using Microsoft.AspNetCore.Mvc;
           using Net3.Data;
           using Net3.Repository;
           using Microsoft.AspNetCore.Authorization;
           using Serilog;
           using Net3.Models.AccountViewModels;

           namespace Net3.Controllers
 {
    [Authorize]
    public class UserController : Controller
{
        ApplicationDbContext _context;

        public UserController(ApplicationDbContext context)
        {
            _context = context;
        }

    public ActionResult Index()
    {
        UserRepository userRepository = new UserRepository(_context);

        UserListingVM usersListing = new UserListingVM();

         var userInfo = _context.Users.Select(u => new UserListingVM()
        {
            Id = u.Id,
            Email = u.Email
        });

         Log.Debug(userInfo.ToList().ToString());

        return View(userRepository.GetAll());
    }
}

}

***UserRepository***

 using Net3.Data;
 using Net3.Models.AccountViewModels;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;

 namespace Net3.Repository
 {
    public class UserRepository
  {
    ApplicationDbContext _context;

    public UserRepository(ApplicationDbContext context)
    {
        this._context = context;
    }


    public IEnumerable<UserListingVM> GetAll()
    {
        var usersListing = _context.Users.Select(u => new UserListingVM()
        {
            Id = u.Id,
            Email = u.Email
        });
        return usersListing;
    }
}
}

This is the wrong way to use Serilog. 这是使用Serilog的错误方法。 Your message template string should be a constant. 您的消息模板字符串应为常量。 It should not change. 它不应该改变。 Read the documentation for further details on that. 阅读文档以获取更多详细信息。

What you should do is convert the users to a list (because we don't want to be pulling it multiple times from the database). 您应该执行的操作是将用户转换为列表(因为我们不想多次从数据库中将其提取)。 Then use a constant format string and pass the list of emails as a parameter. 然后使用恒定格式的字符串并将电子邮件列表作为参数传递。 Do not use string.Join as suggested in the comments. 不要使用string.Join在注释中建议。 Serilog is for logging structured data , and you should take advantage of that. Serilog用于记录结构化数据 ,您应该利用它。 That means you can log an array of strings (or any other type of object!) and have Serilog handle it correctly in most cases. 这意味着您可以记录字符串数组(或任何其他类型的对象!),并在大多数情况下让Serilog正确处理它。

List<UserListingVM> userInfo = _context.Users.Select(u => new UserListingVM()
    {
        Id = u.Id,
        Email = u.Email
    }).ToList();

 List<string> userEmails = userInfo.Select(u => u.Email).ToList();

Log.Debug("Displaying Users {@Users}", userEmails);

Using the Serilog.Sinks.Console sink, I got this output with some test users: 使用Serilog.Sinks.Console接收器,我得到了一些测试用户的输出:

[22:01:24 DBG] Displaying Users ["test@gmail.com", "johndoe@gmail.com", "larryjoe@gmail.com"]

Here's a console app you can do that shows this fully working: 您可以执行以下控制台应用程序,以显示其完全正常工作:

using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console()
            .CreateLogger();

        var users = new List<UserListingVM>
            {
                new UserListingVM
                {
                    Id = 1,
                    Email = "test@gmail.com"
                },
                new UserListingVM
                {
                    Id = 2,
                    Email = "johndoe@gmail.com"
                },
                new UserListingVM
                {
                    Id = 3,
                    Email = "larryjoe@gmail.com"
                },
            };

        var userEmails = users.Select(u => u.Email).ToList();
        Log.Debug("Displaying Users {@Users}", userEmails);
        Console.WriteLine("press any key to exit");
        Console.ReadKey();
    }
}

class UserListingVM
{
    public int Id { get; set; }
    public string Email { get; set; }
}

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

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