see docs:
https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I need to do logging inside the controller methods.
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.
</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();
}
}
Microsoft.ApplicationInsights
NuGet package to the latest stable release.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.