Log4net file is keep appending its fileformat while configured through C# code

16 views Asked by At

I am trying to configure Log4Net through the code and it keeps adding the file format to the file name. Trying to configure the appenders through code as I will need a dynamic path for log files based on certain inputs.

Here is my c# code

 public static class Log4NetConfig
 {     
     public static void GetLogger(string repositoryName, string appName, string siteName, string vrtPath, string filePrefix)
     {
         var repositories = log4net.LogManager.GetAllRepositories().ToList();
         ILoggerRepository repository = repositories.Find(x => x.Name.Equals(repositoryName));

         if (repository == null)
         {
             //Create a new repository
             repository = log4net.LogManager.CreateRepository(repositoryName);
         }

         ConfigureRoot(repository, repositoryName, appName, siteName, vrtPath, filePrefix);
         //ILoggerRepository repository = LogManager.GetRepository(repositoryName);
         //Hierarchy hierarchy = (Hierarchy)repository;
         //hierarchy.Root.RemoveAppender(); // Remove any other appenders
         //hierarchy.Configured = true;

         //log4net.Config.XmlConfigurator.Configure(repository);
     }
     
     private static void ConfigureRoot(ILoggerRepository repository, string repoName, string appName, string siteName, string vrtPath, string filePrefix)
     {
         Hierarchy hierarchy = (Hierarchy)repository;

         if (hierarchy.Root.GetAppender(repoName) == null)
         {
             hierarchy.Root.RemoveAppender(repoName); // Remove any other appenders

             // Iterate over all appenders attached to the root logger
             RollingFileAppender rollingAppender = new RollingFileAppender();

             string logFolderPath = GetLogFolder(appName, siteName, vrtPath);

             rollingAppender.Name = repoName;
             rollingAppender.File = $"{logFolderPath}//";
             rollingAppender.DatePattern = $"yyyyMMdd'.log'";
             rollingAppender.AppendToFile = true;
             rollingAppender.RollingStyle = RollingFileAppender.RollingMode.Composite;
             rollingAppender.MaxSizeRollBackups = 1000;
             rollingAppender.MaximumFileSize = "10MB";
             rollingAppender.StaticLogFileName = false;
             PatternLayout layout = new PatternLayout("%utcdate{yyyy-MM-dd HH:mm:ss.fff} - %-5level -  %property{activityid} - %message%newline");
             rollingAppender.Layout = layout;

             rollingAppender.ActivateOptions(); // Apply the changes

             hierarchy.Root.AddAppender(rollingAppender);
             //hierarchy.AddRenderer(rollingAppender);
             // Set the root logger level

             hierarchy.Root.Level = Level.All;
             hierarchy.Configured = true;
         }
     }
    
     public static string GetLogFolder(string appName, string siteName, string vrtPath)
     {
         var fileLocation = string.Join("\\",
                 new[] { appName, siteName?.Replace(" ", "_"), vrtPath?.Replace("\\", String.Empty) }.Where(c =>
                       !string.IsNullOrEmpty(c)));
         return $@"{Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)}\Nymi\{fileLocation}";
     }
 }

And the output end files are looking like this - enter image description here

Calling this GetLooger function at every Application_BeginRequest event in Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var vPath = HostingEnvironment.ApplicationVirtualPath == "/" ? "" : 
    HostingEnvironment.ApplicationVirtualPath;
    var logConfig = new
    {
       AppName = typeof(WebApiApplication).Assembly.GetName().Name,
       SiteName = HostingEnvironment.SiteName,
       VirtualPath = vPath,
       Prefix = $"{vPath}_"
    };

    Log4NetConfig.GetLogger(logConfig.AppName, logConfig.AppName, logConfig.SiteName, 
        logConfig.VirtualPath, logConfig.Prefix);
}

Note: I am not using XML configuration.

0

There are 0 answers