<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Web.Hosting;
using System.Xml;
using JetBrains.Annotations;
using JetBrains.TeamCity.NuGet.Feed;
using log4net;
using log4net.Config;

[assembly: WebActivator.PreApplicationStartMethod(typeof (Log4Net), "InitApplication")]

namespace JetBrains.TeamCity.NuGet.Feed
{
  public class Log4Net
  {
    [UsedImplicitly]
    public static void InitApplication()
    {
      const string key = "teamcity-dotnet-log-file";
      if (Environment.GetEnvironmentVariable(key) == null)
        Environment.SetEnvironmentVariable(key, Environment.CurrentDirectory);

      new Log4netDefaultLoggerInitializer().InitializeLogging();
    }
  }

  [Serializable]
  public class Log4netDefaultLoggerInitializer 
  {
    private static readonly ILog LOG = LogManagerHelper.GetCurrentClassLogger();

    private const string LOG_ENV_KEY = "teamcity-dotnet-log-file";
    private const string LOG_ENV_PATH = "teamcity-dotnet-log-path";

    public void InitializeLogging()
    {
      LoadConfigFromFile(HostingEnvironment.MapPath("~/Log4Net.xml"));
      AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private static void LoadConfigFromFile(string file)
    {
      var doc = new XmlDocument();

      string config = File.ReadAllText(file);
      var logFileName = GetLogFileName();
      config = config.Replace("${" + LOG_ENV_KEY + "}", logFileName);
      doc.LoadXml(config);

      XmlConfigurator.Configure(doc.DocumentElement);
      LOG.InfoFormat("Started log4net from {0}", file);
    }

    private static string GetLogFileName()
    {
      string destPath = Environment.GetEnvironmentVariable(LOG_ENV_PATH);
      if (destPath == null)
        destPath = Path.Combine(Path.GetTempPath(), "TeamCity.NET");

      if (!Directory.Exists(destPath))
        Directory.CreateDirectory(destPath);


      string id = "NA";
      string sLogId = "NA";
      string sProcessName = "NA";

      try
      {
        Process process = Process.GetCurrentProcess();
        sLogId = process.StartTime.ToString("s").Replace(":", "-").Replace(".", "-");
        // An ISO8601 time string, fitted for file names

        sProcessName = process.ProcessName.Replace(".", "_");
        sProcessName = sProcessName.Substring(Math.Max(sProcessName.Length, sProcessName.Length - 5));
        id = process.Id.ToString();
      }
      catch (Exception e)
      {
        Console.Out.WriteLine("Failed read process information. 'NA' would be used. {0}", e);
      }

      string logFile =
        Path.Combine(destPath,
                     string.Format("{0}.{1}.{2}_{3}-{4}.log", ".net", sLogId, sProcessName,
                                   id, (DateTime.Now - new DateTime(2011, 02, 05, 16, 22, 10)).Ticks));

      string newLogFile = logFile;
      int i = 1;
      while (File.Exists(newLogFile))
      {
        newLogFile = logFile + "." + ++i;
      }

      return newLogFile;
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
      var exception = e.ExceptionObject as Exception;
      if (exception == null)
      {
        LOG.Warn("Unhandled (null) exception in current domain");
        return;
      }

      LOG.WarnFormat("Unhandled exception in current domain: {0}, {1}, {2}", exception, exception.StackTrace,
        exception.InnerException == null ? "(null)" : (object)exception.InnerException);
    }
  }


  public static class LogManagerHelper {
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static ILog GetCurrentClassLogger()<commit_msg>rename nuget server log file name to conform TeamCity log files naming<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Web.Hosting;
using System.Xml;
using JetBrains.Annotations;
using JetBrains.TeamCity.NuGet.Feed;
using log4net;
using log4net.Config;

[assembly: WebActivator.PreApplicationStartMethod(typeof (Log4Net), "InitApplication")]

namespace JetBrains.TeamCity.NuGet.Feed
{
  public class Log4Net
  {
    [UsedImplicitly]
    public static void InitApplication()
    {
      const string key = "teamcity-dotnet-log-file";
      if (Environment.GetEnvironmentVariable(key) == null)
        Environment.SetEnvironmentVariable(key, Environment.CurrentDirectory);

      new Log4netDefaultLoggerInitializer().InitializeLogging();
    }
  }

  [Serializable]
  public class Log4netDefaultLoggerInitializer 
  {
    private static readonly ILog LOG = LogManagerHelper.GetCurrentClassLogger();

    private const string LOG_ENV_KEY = "teamcity-dotnet-log-file";
    private const string LOG_ENV_PATH = "teamcity-dotnet-log-path";

    public void InitializeLogging()
    {
      LoadConfigFromFile(HostingEnvironment.MapPath("~/Log4Net.xml"));
      AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private static void LoadConfigFromFile(string file)
    {
      var doc = new XmlDocument();

      string config = File.ReadAllText(file);
      var logFileName = GetLogFileName();
      config = config.Replace("${" + LOG_ENV_KEY + "}", logFileName);
      doc.LoadXml(config);

      XmlConfigurator.Configure(doc.DocumentElement);
      LOG.InfoFormat("Started log4net from {0}", file);
    }

    private static string GetLogFileName()
    {
      string destPath = Environment.GetEnvironmentVariable(LOG_ENV_PATH);
      if (destPath == null)
        destPath = Path.Combine(Path.GetTempPath(), "TeamCity.NET");

      if (!Directory.Exists(destPath))
        Directory.CreateDirectory(destPath);

      string logFile = Path.Combine(destPath, string.Format("teamcity-nuget-server-{0}.log", (DateTime.Now - new DateTime(2011, 02, 05, 16, 22, 10)).Ticks));

      string newLogFile = logFile;
      int i = 1;
      while (File.Exists(newLogFile))
      {
        newLogFile = logFile + "." + ++i;
      }

      return newLogFile;
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
      var exception = e.ExceptionObject as Exception;
      if (exception == null)
      {
        LOG.Warn("Unhandled (null) exception in current domain");
        return;
      }

      LOG.WarnFormat("Unhandled exception in current domain: {0}, {1}, {2}", exception, exception.StackTrace,
        exception.InnerException == null ? "(null)" : (object)exception.InnerException);
    }
  }


  public static class LogManagerHelper {
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static ILog GetCurrentClassLogger()
{'fraction_non_alphanumeric': 0.11146020347097546, 'fraction_numerical': 0.009874326750448833, 'mean_word_length': 3.3240620957309184, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4877176', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1895, 'n_words': 425}