Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Application Insights Not Running In Local Mode

$
0
0

Hi,

While following the beginners' course on MVA I came to know about Application Insights being available for running in local mode. Upon adding the same to my sample project I was surprised to find the activity to be showing 0's (zeros)?

  1. I right clicked the project
  2. Selected Add > Application Insights Telemetry
  3. Upon Application Insights Configuration Page selected Start Free
  4. On the proceeding page I selected the option Or just add SDK to try local only mode at the bottom of page
  5. Ran the application by pressing IIS Express
  6. The Application Insights button I had added on the toolbar didn't show any number of activity
  7. Upon clicking the said button the Application Insights Search page appeared, but with every event/result as 0 (Zero)

While the Immediate Window showing Application Insights Telemetry (unconfigured) at some places.

The Startup file is as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace FAQ
{
    public class Startup
    {
        public Startup(IConfiguration config)
        {
            configuration = config;
        }

        public IConfiguration configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<FAQ_TempDB>(options => options.UseInMemoryDatabase("Some_Name"));
            services.AddLogging();
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
            app.UseStaticFiles();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

while I have used it on the Create Model in a single CRUD page model as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using FAQ.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace FAQ.Pages
{
    public class CreateModel : PageModel
    {
        private readonly FAQ_TempDB _TempDB;

        private ILogger<CreateModel> Log;

        public CreateModel(FAQ_TempDB tempDB, ILogger<CreateModel> log)
        {
            _TempDB = tempDB;
            Log = log;
        }

        [TempData]
        public string xMessage { get; set; }

        [BindProperty]
        public Worker Worker { get; set; }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid) {return Page();}

            _TempDB.Workers.Add(Worker);
            await _TempDB.SaveChangesAsync();
            var xMsg = $"Record of {Worker.W_FName} added!";
            xMessage = xMsg;
            Log.LogCritical(xMsg);
            return RedirectToPage("/BackToDB");
        }
    }

    public class BackToDBModel : PageModel
    {
        private readonly FAQ_TempDB _TempDB;

        public BackToDBModel(FAQ_TempDB tempDB)
        {
            _TempDB = tempDB;
        }

        public IList<Worker> Workers { get; private set; }

        [TempData]
        public string xMessage { get; set; } //Name should be same as one declared in Create Model

        public async Task OnGetAsync()
        {
            Workers = await _TempDB.Workers.AsNoTracking().ToListAsync();
        }

        public async Task<IActionResult> OnPostDeleteAsync(int id)
        {
            var worker = await _TempDB.Workers.FindAsync(id);
            if (worker != null)
            {
                _TempDB.Workers.Remove(worker);
                await _TempDB.SaveChangesAsync();
            }
            return RedirectToPage();
        }
    }

    public class EditModel : PageModel
    {
        private readonly FAQ_TempDB _TempDB;

        public EditModel(FAQ_TempDB tempDB)
        {
            _TempDB = tempDB;
        }

        [BindProperty]
        public Worker Worker { get; set; }

        public async Task<IActionResult> OnGetAsync(int id)
        {
            Worker = await _TempDB.Workers.FindAsync(id);
            if (Worker == null)
            {
                return RedirectToPage("/BackToDB");
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _TempDB.Attach(Worker).State = EntityState.Modified;

            try
            {
                await _TempDB.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                throw new Exception($"Worker {Worker.W_FName} Not Found!", ex);
            }
            return RedirectToPage("/BackToDB");
        }
    }
}

The app works well on console reflecting the requisite critical portion in red but I don't understand why not on the local mode of Application Insights? Is it necessary to use Azure?

Please see if any of you experts can help.

Thanks


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>