How to do it..

  1. Open the terminal and navigate to the root of your application built in the previous recipe.
  2. The directory structure should look like this:
  1. Now type the following command and press Enter in the terminal to create the new ASP.NET Core MVC application:
      $ dotnet new mvc -o Chapter3.LogFile.LogAppMvc
  1. Let's add this new project to the solution:
      $ dotnet sln add 
Chapter3.LogFile.LogAppMvc/Chapter3.LogFile.LogAppMvc.csproj
  1. Now let's perform a build to check that everything is working fine:
      $ dotnet build
  1. Now, navigate to the app we just created:
      $ cd Chapter3.LogFile.LogAppMvc/
  1. Execute the application with the following command:
      $ dotnet run
  1. If everything is fine, open your browser and type http://localhost:5000 in the address bar and hit Enter. The browser output should look like this:
  1. Let's close the browser and, in the terminal window, hit Ctrl + C to stop the web server.
  2. We need to add the reference to our .NET Standard 2.0 library we built before. Let's add it now using this command in the terminal:
      $ dotnet add reference 
../Chapter3.LogFile.LogLib/Chapter3.LogFile.LogLib.csproj
  1. Let's get back to the root folder by typing the following command:
      $ cd ..
  1. Now, in the root of the solution directory, type the following command to open Visual Studio Code using the current directory:
      $ code . 
  1. In Visual Studio Code, expand the Chapter3 label and expand Chapter3.LogFile.LogAppMvc.
  2. Again, expand the Controllers folder and click on HomeController.cs:

  1. In HomeController.cs, add the following using directive to the last line of the using directives block:
      using Chapter3.LogFile.LogLib;
  1. Now, next to the starting curly bracket of the Index method of the HomeController class, add the following code:
      TextLog logFile = new TextLog();
logFile.WriteLog("You are in the Index action.");
logFile.CloseLog();
  1. Let's add more code inside the About method too:
      TextLog logFile = new TextLog();
logFile.WriteLog("You are in the About action.");
logFile.CloseLog();
  1. Do the same to the Contact method as well:
      TextLog logFile = new TextLog();
logFile.WriteLog("You are in the Contact action.");
logFile.CloseLog();
  1. Now we are done with adding code, let's navigate to Chapter3.LogFile.LogAppMvc:
      $ cd Chapter3.LogFile.LogAppMvc/
  1. Let's run the application:
      $ dotnet run
  1. Open your favorite browser, type localhost:5000 in the address bar, and press Enter. Click on the navigation links for Home, About, and Contact a few times.
  2. Close the browser.
  1. Now go to Visual Studio Code, expand the Chapter3 label, and expand Chapter3.LogFile.LogAppMvc. You should see a file named server_log.txt in the root:
  1. Now click on the server_log.txt filename to see the output:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.134