Continuous Integration for ASP.NET Web App with VSTS and Git

Continuous Integration(CI) is not a new topic to the Software Developers, yet there are companies picking up the trend. CI will help you to commit and push your code at the convenience of your IDE (Visual Studio 2017 in my case) to Visual Studio Team Services Code Repository and trigger a build. Your build can be defined in the manner you want it to be by adding different tasks such as restoring NuGet Packages, running unit tests etc. You can order your build to copy the build output to a given path. This is much easier when you have a team working on the same project as it avoids last minute integrations.

CI History
Build history in VSTS

If you are totally new to CI/CD you can read this to get an understanding about the topic. You can read this to get CI running for your ASP.NET web app, using Git and Visual Studio Team Services.

Git and Team Services made easy with Visual Studio 2017

Git distributed version control system can be used to manage code in the Visual Studio Team Services repository. Visual Studio 2017 IDE provides easy access to Git and VSTS.

Here are some important information to begin with.

If you come across error 400 when you are cloning, first of all check whether you have spaces in the git repository URL and replace the spaces with %20 as suggested here.

How to add Log4Net to your ASP.Net MVC (C#) project

  1. Add log4net dll to your references using Nuget Package Manager.
  2. Add this line to your AssemblyInfo.cs file
    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
  3. Add the following xml code to your Web.config file, inside the configurations tags. The value for version should be the version of your log4net dll.
    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
    </configSections>
  4. Also add the following lines to the Web.config file inside the configuration tags. Here I am logging to a file called “Jumppage.log” inside a folder named “Log”.
    <!-- Log4net Logging Setup -->
    <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
    <file value="Log\Jumppage.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="FATAL" />
    </filter>
    </appender><root>
    <level value="DEBUG" />
    <appender-ref ref="FileAppender" />
    </root>
    </log4net>
    
  5. Go to your class file and add the namespace, log4net as follows.
    using log4net;
  6. Add following code inside your class. You need to write your class name inside the typeof() method. In this project my class name is “AnnouncementController”.
    private static readonly ILog Logger = LogManager.GetLogger(typeof(AnnouncementController));
  7. Then you can log your error as follows.
    Logger.Error("Error in posting announcements. error message:"+ ex.Message + ", stacktrace:" + ex.StackTrace);