How to get IIS site name in NLog

I'm trying to log the site name of multiple IIS sites, dynamically, without providing the name as a variable for each site individually. My NLog configuration is centralized, meaning that every site refers to the same NLog configuration file. Also, I can't make changes to the sites themselves, I can only modify the configuration file.

When dealing with, for example, a WinForms application, I can use the ${processname} layout renderer. For an IIS site, however, this will return the ISS application pool process name, like w3wp. But this doesn't allow me to differentiate between multiple IIS sites.

My current solution is checking whether the process name is w3wp, and then using the ${windows-identity} layout renderer, as this will return the name of the application pool when the Identity is set to ApplicationPoolIdentity. But this is not always possible/wanted.

I've also been looking at using ${basedir}, but I can't find a way to strip any unnecessary parts, as I would only need the last folder of the path.

Any ideas?

Answers


I don't know if there is a built-in feature to know this kind of information, but you could create your own LayoutRenderer, the code to create it is as follows:

namespace NLog.Extensions.LayoutRenderers
{
    using System.Text;
    using NLog.LayoutRenderers;

    [LayoutRenderer("IISSiteName")]
    public class IISSiteName : LayoutRenderer
    {
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            if (System.Web.Hosting.HostingEnvironment.ApplicationHost != null)
                builder.Append(System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName());
        }
    }
}

You must compile this code and add the DLL using the tag in your NLog.config file as follows

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" >

  <extensions>
    <add assemblyFile="c:\\your-path-to-dll" />
  </extensions>

  <targets>
    <target xsi:type="File" name="file" fileName="${basedir}\file.txt" layout="${date} ${level} ${IISSiteName} "/>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />
  </rules>

</nlog>

if you add the compiled DLL as reference to your project you must change the assemblyPath for assembly and add it as a Namespace.Class format.

I made it work on my PC, so it should work correctly. Tell me if not.

Greetings


Need Your Help

Google Places API for iOS vs Google places Web service

ios objective-c web-services google-maps google-places-api

I plan to have an application to list nearby places in a UITableView with UISearchController combined by Auto-complete feature to filter the result of places and I'm trying to use Google Places API...

Why are some standard ocaml libraries opam packages and some are not?

ocaml opam ocamlfind

I see the base-unix package in the OCaml opam repository . It is described as the "Unix library distributed with the OCaml compiler". Some other libraries that are also distributed with the OCaml

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.