.NET: Using A Single log4net Configuration File Across A Multi-Tier Web Application

log4net is
a tool to help the programmer output log statements to a variety of output
targets.



This
example is written in Visual Studio 2015 for a WebAPI2 solution.  The sample solution consists of three projects
that include a project for data, models, unit tests, etc.  Source code can be found here:



https://github.com/cjsander/Log4netInMultiTierWebApp

SETUP:
1.     
Create
your project and then import the following Nuget package:        
https://www.nuget.org/packages/log4net/
2.     
For
each project, add reference to log4net.dll assembly.  Though all configuration will be in the  main
API project, other projects will require the assembly reference as well.
3.     
Configuration
files to be included are:
a.       Log4net.config
b.      Web.config
c.       Settings.xml
d.      Global.asax.cs
4.     
Note
that the <appsettings> node of the web.config file has been abstracted
into a separate file — Settings.xml.  
These settings will be referenced in the web.config in the following
line of code:

         <appSettings file=c:webappslog4sampleconfigsettings.xml/>

APPLICATION:
1.       In your web.config file, enter the
following:
  <configSections>
    <section name=log4net type=log4net.Config.Log4NetConfigurationSectionHandler,
log4net
/>
  </configSections>
2.       Now, add the logging configuration
values.  These can be added in your
web.config(or app.config) but in this scenario, it has its own configuration
file – log4net.config:
<log4net>
 
<
appender name=RollingFile type=log4net.Appender.RollingFileAppender>…..
</log4net>

3.       Then,
you will want to add the path to the file in your <appSettings> node:
<add key=logPath value=c:webappslog4sampleconfiglog4net.config />
4.       In your
Global.asax file, add the following to the Application_Start() method:
//log4net setup:
var logPath = (ConfigurationManager.AppSettings[“logPath”]);

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(logPath));

EXTRA:
You can also
add a helper method to customize your logging and add details that would help
with your debugging:
public class Log4NetHelper
    {
public string LogMsg(Message m, [Optional] string exceptionMsg, [CallerMemberName] string methodName = “”)
        {
            string msg = $”{methodName} {m} {exceptionMsg};
            return msg;
        }
}

               To
reference:
private readonly Log4NetHelper _msg = new Log4NetHelper();
_logger.Debug(_msg.LogMsg(Log4NetHelper.Message.InitialRequestStart));


SAMPLE FOLDER STRUCTURE:





.NET : Adding custom ASP.NET WebApi Help Page

1.  Add annotations to model, controller, etc. using System.ComponentModel.DataAnnotations
2.  Navigate to project “Properties” –> “Build” –> Output
3.  Check “XML Documentation file:”, then type in a name for the help document xml, and save
4.  In the solution, navigate to “Areas” –> “App_Start” –> “HelpPageConfig.cs”
5.  Uncomment the “config.SetDocumentationProvider…” line, add your created name for the help document xml, and save:
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath(“~/App_Data/DevOpsTaskHelp.XML”)));
6.  Build solution, navigate to help page “API”, and your annotations should be present.

Adding XML WebApi Comments To Swagger:
1.  Install-Package “Swashbuckle”
2.  Open the “SwaggerConfig.cs” file and uncomment the following line:
c.IncludeXmlComments(GetXmlCommentsPath());
3.  Generate method for “GetXmlCommentsPath()”
private static string GetXmlCommentsPath()
        {
            return System.String.Format(@”{0}binDevOpsTaskHelp.XML”, System.AppDomain.CurrentDomain.BaseDirectory);
        }

REFERENCE(S):
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages
https://blogs.msdn.microsoft.com/yaohuang1/2012/09/30/asp-net-web-api-help-page-part-1-basic-help-page-customizations/