How to do it...

  1. Open Visual Studio 2017.
  2. Click File | New | Project to create a project. 
  3. In the New Project dialog box, expand the Other Project Types node in the left-hand pane and select Visual Studio Solutions. In the right-hand pane, select Blank Solution.
  4. In the Name: textbox, type Chapter10.Logging, and, in the Location: textbox, select a path from the drop-down box or click on the Browse... button to locate a path:
  1. Click OK.
  2. Now, the Solution Explorer (Ctrl + Alt + L) should look like this:
  1. Now, right-click on the Chapter10.Logging label in the Solution Explorer and select Add | New Project.
  2. In the New Project dialog box, expand the Visual C# node.
  3. Select .NET Standard in the left-hand pane and Class Library (.NET Standard) in the right-hand pane:
  1. In the Name: textbox, type Chapter10.Logging.LogLib, leave the other defaults as they are, and click OK:
  1. Now, the Solution Explorer (Ctrl + Alt + L) should look like this:
  1. Now, select Class1.cs in the Solution Explorer and press F2 to rename the file LoggerDemo.cs.
  2. Answer Yes in the confirmation dialog box that asks to rename the class name as well.
  3. Double-click on the LoggerDemo.cs label in the Solution Explorer.
  4. Let's scroll up in the code window and add the following using directive:
      using System.IO;
  1. Now, create two class-level variables to hold the filename and the StreamWriter class:
      private string logFileName = "lib_log.txt";
private StreamWriter logFile;
  1. Now, create the default constructor method as follows:
      public LoggerDemo()
{
WriteLog("Constructor Called.");
}
  1. Again, create this private method to write log details to the file: 
      private void WriteLog(string message)
{
if (!File.Exists(logFileName))
{
logFile = File.CreateText(logFileName);
}
else
{
logFile = File.AppendText(logFileName);
}

logFile.WriteLine($"{DateTime.Now} Log Message: {message} ");
logFile.Close();
}
  1. Create these three public methods:
      public void CallMethod1()
{
WriteLog("Method 1 Called");
}

public void CallMethod2()
{
WriteLog("Method 2 Called");
}

public void CallMethod3()
{
WriteLog("Method 3 Called");
}
  1. Press Ctrl + Shift + B to build the solution.
..................Content has been hidden....................

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