Home Display user email in SeriLog ASP.NET
Reply: 1

Display user email in SeriLog ASP.NET

Chris H.
1#
Chris H. Published in 2018-01-12 22:50:01Z

I am logging data into SeriLog using .Net Cli. 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. 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: P.S. I'm pretty new at this. Thank you in advance.

*****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;
    }
}
}
mason
2#
mason Reply to 2018-02-06 04:03:32Z

This is the wrong way to use 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. Serilog is for logging structured data, and you should take advantage of that. That means you can log an array of strings (or any other type of object!) and have Serilog handle it correctly in most cases.

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:

[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; }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.350551 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO