Logging in Asp .Net Core 2.0


Logging is very important part of any application as almost all application requires proper logging.

.Net Core 2.0 has some simplified way of implementing logging.

Let us see what are the changes required to enable logging in .Net Core 2.0 application.

First of all, we need to make changes in appsettings.json file as below:

  "Logging": {
    "IncludeScopes": false,
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"

Here we have logger provider along with the levels so it allows us to configure logging levels per logging provider.

Previously, the entry in appsettings.json would only be applicable to the console logger but now the entry in the appsettings.json  has logger provider names in it like Debug, EventLog, TraceSource etc.

Do not forget to make these changes as it is required and it may throw the exception if you have missed this. I saw some StackOverflow questions mainly on this where people forgot to add related details in appsettings.json file.

Once it is done, in Main class, we can configure the logging details as shown below:

public static void Main()
  var host = new WebHostBuilder()
               .ConfigureAppConfiguration((hostingContext, config) =>
       var env = hostingContext.HostingEnvironment;
       config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
       .AddJsonFile("hosting.json", optional: false)
       .ConfigureLogging((webhostContext, builder) => {
.UseIISIntegration() .UseStartup() .UseApplicationInsights() .Build(); host.Run(); }

So we can now move the logging configuration part into the WebHostBuilder.

In above code, concentrate on line:


This method sets up logging to look for configuration in a Logging section, in this case, we can write in appsettings.Development.json file as below:

 "Logging": {
   "IncludeScopes": false,
   "Debug": {
   "LogLevel": {
   "Default": "Information"
 "LogLevel": {
   "Default": "Debug"

After this, you can directly inject logger service into the controller as shown below:

private readonly IRepository _repository;
private readonly ILogger _logger;

public MyController(IRepository repository,
ILogger logger)
    _repository = repository;
    _logger = logger;

Now in your controller class, wherever you want to log for Information then it should be:


And if we want to log for warning then:


Same goes for other levels as well.

Exception logging:

The logger method allows us to pass an exception as below:

catch (Exception ex) {
     _logger.LogWarning(LoggingEvents.GetItemNotFound, ex, "Error");

Please note that different providers handle the exception information in different ways.

Some useful StackOverflow QAs:




SPA templates(Angular, React etc) with ASP .NET Core 2.0

In my last post, I have explained about the setup required for .Net Core 2.0 which you can find here.

Along with Core 2.0, there are some newly added default templates has been added by Microsoft team.

The major attraction is the SPA templates that are shipped with Core 2.0.

SPA templates like Angular, React.js, React.js and Redux etc can be seen in default template page:


These three templates create actually the same sample application but the difference is in JavaScript libraries. Notice that these new templates are available only when ASP.NET Core 2 is selected from framework dropdown on top of the dialog.

The best part is, JavaScript Services functionality is used by these SPA templates to embed Node.js within .Net Core on the server. Thus it compiles the Javascript applications server-side as part of .Net build.

The advantage of having SPA templates in the default template section is, most of the required scripts, dlls and other required components are already added in the application so you can start building your application without worrying about the dependencies.

Setup for Asp .Net Core 2.0


Microsoft has announced Core 2.0 and it has some awesome features which I will explain in future blog posts.

For now, let us see how to converts your Core 1.0 application into Core 2.0

First of all, we will change the .Net core version in global.json file as shown below:

   "sdk": {
   "version": "2.0.0-preview3-006912"

After that we need to change the target library to Core 2.0 as shown below:


Also, change and update below line in .csproj file:


Once it is done, we need to add below package in the .csproj file:


<PackageReference Include=“Microsoft.AspNetCore.All” Version=“2.0.0” />


This new metapackage contains references to all of the AspNetCore packages and maintains a complete line-up of compatible packages.  You can still include explicit references to specific Microsoft.AspNetCore.* package versions if you need one that is outside of the lineup

Now we need to change PackageTargetFallback in same .csproj file as below:


Here we added AssetTargetFallback instead of earlier PackageTargetFallback

Now let us jump to the Main method in Program.cs class. In Core 1.0 we used to add different extended methods like .UseKestrel(), .UseIISIntegration() etc.

For Core 2.0 it is more simplified as shown below:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace AspNetCoreDotNetCore2._0App
   public class Program
     public static void Main(string[] args)

   public static IWebHost BuildWebHost(string[] args) =>

Now your application is converted into Core 2.0 from Core 1.0

Amplitude 2017: A MasterCard Hackathon


MasterCard is going to conduct one of the biggest hackathons of India on November. Name of the hackathon is Amplitude 2017.

The theme of this Hackathon is #CashLessBanoIndia. Name of the theme is self-explanatory, the main focus of this hackathon is to come up with the ideas which will encourage people to use digital payments instead of Cash.

There are mainly 4 themes for this hackathon which includes creating meta data driven rules platform, how to accelerate the adoption of digital payments in India, To develop alternate payment solution for merchants and increase the security of P2P and P2M transactions.

MasterCard has some awesome products which can be used for this hackathon. By the way, developers can use any API or products they want for the hackathon.

Below is the process required for Amplitude 2017:


Some useful links:

For Registration: https://www.hackerearth.com/sprints/amplitude-2017/

To know the rules: https://www.hackerearth.com/sprints/amplitude-2017/rules/#rules

To know about the themes: https://www.hackerearth.com/sprints/amplitude-2017/custom-tab/themes/#Themes

So if you are a geek and want to do something for the nation then just register yourself and be a part of the biggest hackathon of MasterCard.


Basic information for Ready API (SoapUI): API Testing


What is SoapUI?

SoapUI is an open-source web service testing application for service-oriented architectures (SOA) and representational state transfers (REST). Its functionality covers web service inspection, invoking, development, simulation and mocking, functional testing, load and compliance testing.

Basic kick off with SoapUI:

Once you get SoapUI installed, it will have the dashboard as shown below:


To add sample test, we need to select between SOAP or REST:


We will select URL as we want to test REST API. Once URL is selected, we need to provide the URL we need to test. For example I am using StackOverflow public API to get the users:


Once we click on Next, it will create one new project and in that project it will create a TestSuite where all your test will reside:


Once we run the test, it will show green or Red color whether the test is passed or failed:


If the test is passed, it will show the response in JSON which we have got with REST GET request:


Now as it is mentioned in very first image, if we perform some security tests then it will ask about the things we need to add in security test as shown below:


Once we click on Finish, it will show the result as below:


We can get detailed report as well:


I created one sample Web API on my machine which returns 2 values and I published the API project. When I gave localhost URL in ReadyAPI, It created one new project in ReadyApi and it gave the result as shown below:


So if the API would be installed on IIS then also APIs can be tested using Ready Api.

As it works with GET, it can work with POST\PUT\DELETE as well:


Virtual service:

SoapUI provides a feature called Virt in which we can create a virtual service, so if testers want to mock the service without calling the actual REST API, it is possible with SoapUI. We need to create a new Virt as below:


This Virt can behave as GET\POST etc as per your need and Once the Virt is created, we can even host that Virt on localhost by applying some settings.

To conclude this, if we want to test the scenario of a user account creation, we will create one project and in that there would be a test suite named “Test account creation” and in that suite we can add different test cases as shown below:


Try it out.


Infosys interview questions for experienced .Net developers


Below are some of the most important questions which were asked during Infosys interview recently.

  • Tell me about yourself

This is the best place where you can showcase yourself. Tell as much as you can about yourself which should highlight all your achievements but that too in very short words. Do not stretch it for so long that interviewer gets bored.

  • Explain the architecture of your current project


  • Write a program to swap 2 numbers, if you will be able to achieve it using the third variable then he would ask it to di it without using the third variable



  • Tell everything about OOPS

In this, you should tell everything you know about OOPS, each and every concept which you know

  • How to achieve load balancing in SQL Server


  • Tell me about the SDLC methods you have used

You can explain each and everything about Agile here

  • Different methods for passing data from controller to view in MVC


  • Explain different state management techniques in .Net

You can explain all state management techniques of Web forms and also explain the same for MVC



  • If you reset IIS then will it reset all application data?
  • How to know, how many times an object is initiated?

By using a static counter in the constructor. Explain by writing code

  • Explain the life cycle of .Net


  • Explain the life cycle of MVC


  • Explain all the joins in SQL


  • Explain about Clustered and Non clustered indexes in SQL


  • Difference between function overloading and function overriding


  • What are Telerik controls, and why do we require them?


  • What is Ajax and write a sample code of Ajax call in Javascript or jQuery


  • Difference between stored procedure and functions


  • How jQuery is better than Javascript?


I will be adding new questions if I get to know it from somone, meanwhile you may also add them in below comments.



Query string in .Net core and MVC 6


As you all know that from MVC6 we are seeing some changes in our regular coding techniques.

We were using below lines in MVC5 for getting values from query string:

var myQueryString = Request.QueryString[“myQueryString”]

But in MVC6, above lines will throw errors in your code.

So, you might be wondering how can we achieve this in MVC6.

Below is the way:

We can use Request.Query which is new in ASPNET 5 and MVC6.

var myQueryString = Request.Query;

For example, you have an URL like:


So to get all keys we will write below code:

var myQueryString = Request.Query;

Here myQueryString.keys contains all the keys which are there in your query string.

Now to take those keys out one by one, we will write below code:

foreach(var key in myQueryString.Keys)
   //// Do the operations as per your need with the key

That is it. You can easily get your values from the Query string in MVC6 and .Net core.