How to configure application insight logins in Asp .Net Mvc applciation. (Not in .Net Core)

Athula Chandrawansha 20 Reputation points
2024-06-05T13:36:00.81+00:00

I need to do logging inside the controller methods.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,379 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 59,306 Reputation points
    2024-06-05T18:20:46.34+00:00
    0 comments No comments

  2. Lan Huang-MSFT 27,956 Reputation points Microsoft Vendor
    2024-06-06T05:36:34.3766667+00:00

    Hi @Athula Chandrawansha,

    How to configure application insight logins in Asp .Net Mvc applciation. (Not in .Net Core)

    Initially, you need to add Application Insights. Below are the steps you can follow to add Application Insights to your Application.

    1. Select Project > Add Application Insights Telemetry > Application Insights Sdk (local) > Next > Finish > Close.
    2. Open the ApplicationInsights.config file.
    3. Before the closing </ApplicationInsights> tag, add a line that contains the connection string for your Application Insights resource. Find your connection string on the overview pane of the newly created Application Insights resource. XMLCopy
         <ConnectionString>
      
      And while logging in from your Home Controller try:
         public class HomeController : Controller
         {
             ILogger<HomeController> _logger;
                 public HomeController(ILogger<HomeController> logger)
                 {
                     _logger = logger;
                 }
         
                 public ActionResult Index()
                 {
                     _logger.LogInformation("Log message in the Index() method");
                     return View();
                 }
         }
      
    4. Select Project > Manage NuGet Packages > Updates. Then update each Microsoft.ApplicationInsights NuGet package to the latest stable release.
    5. Run your application by selecting IIS Express. A basic ASP.NET app opens. As you browse through the pages on the site, telemetry is sent to Application Insights.

    If Adding Application Insights Automatically isn't working, then you can try manually from HERE.

    I need to do logging inside the controller methods.

    You can also create your own class that inherits from ActionFilterAttribute and then override the OnActionExecuting method.

    Example

    public class LogActionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
    
            var controller = filterContext.RequestContext.RouteData.Values["Controller"];
            var action = filterContext.RequestContext.RouteData.Values["Action"];
    
            //
            // Perform logging here
            //
    
            base.OnActionExecuting(filterContext);
        }
    }
    
    public class HomeController : Controller
    {
        [LogAction]
        public ActionResult Index()
        {
    
            return View();
        }
    
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments