From 50727debc72758f590ea4e483e8960b9cd1490fc Mon Sep 17 00:00:00 2001
From: Bavarian <Bavarian@Team-MediaPortal.com>
Date: Sun, 1 Apr 2012 20:07:53 +0200
Subject: [PATCH 1/3] First implementation with Common Logger in "Common
 MP-TV3/common.Utils/Logger"

Signed-off-by: Bavarian <Bavarian@Team-MediaPortal.com>
---
 Common-MP-TVE3/Common.Utils/Common.Utils.csproj    |    8 +
 .../Common.Utils/Logger/CommonLog4NetLogger.cs     |  322 ++++++++++++++++++++
 .../Common.Utils/Logger/CommonLogLevel.cs          |   17 ++
 .../Common.Utils/Logger/CommonLogType.cs           |   22 ++
 Common-MP-TVE3/Common.Utils/Logger/CommonLogger.cs |    7 +
 .../Common.Utils/Logger/ICommonLogger.cs           |   87 ++++++
 TvEngine3/TVLibrary/TVServer.Base/log4net.config   |   16 +-
 .../TVLibrary/TvLibrary.Interfaces/Logging/Log.cs  |   15 +-
 .../TvLibrary.Interfaces.csproj                    |    4 +
 TvEngine3/TVLibrary/TvService/Service1.cs          |   12 +
 mediaportal/MediaPortal.Base/log4net.config        |   16 +-
 .../Utils/ServiceImplementations/log4netLogger.cs  |  172 -----------
 .../Utils/Services/GlobalServiceProvider.cs        |   15 +-
 mediaportal/Utils/Utils.csproj                     |    8 +-
 mediaportal/WatchDog/PostTestActions.cs            |    4 +-
 15 files changed, 532 insertions(+), 193 deletions(-)
 create mode 100644 Common-MP-TVE3/Common.Utils/Logger/CommonLog4NetLogger.cs
 create mode 100644 Common-MP-TVE3/Common.Utils/Logger/CommonLogLevel.cs
 create mode 100644 Common-MP-TVE3/Common.Utils/Logger/CommonLogType.cs
 create mode 100644 Common-MP-TVE3/Common.Utils/Logger/CommonLogger.cs
 create mode 100644 Common-MP-TVE3/Common.Utils/Logger/ICommonLogger.cs
 delete mode 100644 mediaportal/Utils/ServiceImplementations/log4netLogger.cs

diff --git a/Common-MP-TVE3/Common.Utils/Common.Utils.csproj b/Common-MP-TVE3/Common.Utils/Common.Utils.csproj
index d289637..a8d4976 100644
--- a/Common-MP-TVE3/Common.Utils/Common.Utils.csproj
+++ b/Common-MP-TVE3/Common.Utils/Common.Utils.csproj
@@ -71,6 +71,9 @@
     <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="log4net">
+      <HintPath>..\..\mediaportal\MediaPortal.Base\log4net.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core">
       <RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -87,6 +90,11 @@
     <Compile Include="Compatibility\CompatibleVersionAttribute.cs" />
     <Compile Include="Compatibility\SubsystemVersionAttribute.cs" />
     <Compile Include="Compatibility\UsesSubsystemAttribute.cs" />
+    <Compile Include="Logger\CommonLog4NetLogger.cs" />
+    <Compile Include="Logger\CommonLogger.cs" />
+    <Compile Include="Logger\CommonLogLevel.cs" />
+    <Compile Include="Logger\CommonLogType.cs" />
+    <Compile Include="Logger\ICommonLogger.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
diff --git a/Common-MP-TVE3/Common.Utils/Logger/CommonLog4NetLogger.cs b/Common-MP-TVE3/Common.Utils/Logger/CommonLog4NetLogger.cs
new file mode 100644
index 0000000..be989fe
--- /dev/null
+++ b/Common-MP-TVE3/Common.Utils/Logger/CommonLog4NetLogger.cs
@@ -0,0 +1,322 @@
+﻿using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Xml;
+using log4net;
+
+namespace MediaPortal.Common.Utils.Logger
+{
+  public class CommonLog4NetLogger: ICommonLogger
+  {
+
+    #region variables
+    protected CommonLogLevel MinLevel = CommonLogLevel.All;
+    protected Dictionary<CommonLogType, ILog> _loggers;
+    #endregion
+
+    #region Constructors/Destructors
+    public CommonLog4NetLogger(string loggerName, string dataPath, string logPath)
+    {
+      var logFile = Path.Combine(dataPath, "Log4Net.config");
+      if (!File.Exists(logFile)) 
+        WriteDefaultConfig(logFile);
+
+      var xmlDoc = new XmlDocument();
+      var fs = new FileStream(Path.Combine(dataPath, "Log4Net.config"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+      xmlDoc.Load(fs);
+      fs.Close();
+      
+      var nodeList = xmlDoc.SelectNodes("configuration/log4net/appender/file");
+      if (nodeList != null)
+        foreach (var node in nodeList.Cast<XmlNode>().Where(node => node.Attributes != null)) {
+          if (node != null)
+            if (node.Attributes != null)
+              foreach (var attribute in node.Attributes.Cast<XmlAttribute>().Where(attribute => attribute.Name.Equals("value")))
+              {
+                if (attribute.Value != null)
+                  attribute.Value = Path.Combine(logPath, Path.GetFileName(attribute.Value)).Replace("[Name]", loggerName);
+                break;
+              }
+        }
+      var mStream = new MemoryStream();
+      xmlDoc.Save(mStream);
+      mStream.Seek(0, SeekOrigin.Begin);
+      log4net.Config.XmlConfigurator.Configure(mStream);
+
+      _loggers = new Dictionary<CommonLogType, ILog>();
+      foreach (CommonLogType logType in Enum.GetValues(typeof(CommonLogType)))
+      {
+        _loggers.Add(logType, LogManager.GetLogger(logType.ToString()));
+      }
+    }
+
+    #endregion
+
+    #region WriteDefaultConfig
+    private void WriteDefaultConfig(string logFile)
+    {
+      TextWriter tw = new StreamWriter(logFile);
+      tw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
+      tw.WriteLine("<configuration>)");
+      tw.WriteLine("  <configSections>");
+      tw.WriteLine("    <section name=\"log4net\" type=\"log4net.Config.Log4NetConfigurationSectionHandler, log4net\"/>");
+      tw.WriteLine("  </configSections>");
+      tw.WriteLine(" ");
+      tw.WriteLine("  <log4net>");
+      tw.WriteLine("    <appender name=\"DefaultLogAppender\" type=\"log4net.Appender.RollingFileAppender\">");
+      tw.WriteLine("      <file value=\"[Name].log\" />");
+      tw.WriteLine("      <appendToFile value=\"true\" />");
+      tw.WriteLine("      <rollingStyle value=\"Once\" />");
+      tw.WriteLine("      <maxSizeRollBackups value=\"4\" />");
+      tw.WriteLine("      <maximumFileSize value=\"1MB\" />");
+      tw.WriteLine("      <staticLogFileName value=\"true\" />");
+      tw.WriteLine("      <layout type=\"log4net.Layout.PatternLayout\">");
+      tw.WriteLine("        <conversionPattern value=\"[%date] [%-7logger] [%-9thread] [%-5level] - %message%newline\" />");
+      tw.WriteLine("      </layout>");
+      tw.WriteLine("    </appender>");
+      tw.WriteLine(" ");
+      tw.WriteLine("    <appender name=\"ErrorLogAppender\" type=\"log4net.Appender.RollingFileAppender\">");
+      tw.WriteLine("      <file value=\"[Name]-Error.log\" />");
+      tw.WriteLine("      <appendToFile value=\"true\" />");
+      tw.WriteLine("      <rollingStyle value=\"Once\" />");
+      tw.WriteLine("      <maxSizeRollBackups value=\"4\" />");
+      tw.WriteLine("      <maximumFileSize value=\"1MB\" />");
+      tw.WriteLine("      <staticLogFileName value=\"true\" />");
+      tw.WriteLine("      <layout type=\"log4net.Layout.PatternLayout\">");
+      tw.WriteLine("        <conversionPattern value=\"[%date] [%-7logger] [%-9thread] [%-5level] - %message%newline\" />");
+      tw.WriteLine("      </layout>");
+      tw.WriteLine("    </appender>");
+      tw.WriteLine(" ");
+      tw.WriteLine("    <appender name=\"ErrorLossyFileAppender\" type=\"log4net.Appender.BufferingForwardingAppender\">");
+      tw.WriteLine("      <bufferSize value=\"1\" />");
+      tw.WriteLine("      <lossy value=\"true\"/>");
+      tw.WriteLine("      <evaluator type=\"log4net.Core.LevelEvaluator\">");
+      tw.WriteLine("      <threshold value=\"ERROR\" />");
+      tw.WriteLine("      </evaluator>");
+      tw.WriteLine("      <appender-ref ref=\"ErrorLogAppender\" />");
+      tw.WriteLine("    </appender>");
+      tw.WriteLine(" ");
+      tw.WriteLine("    <appender name=\"ConsoleAppender\" type=\"log4net.Appender.ConsoleAppender\">");
+      tw.WriteLine("      <layout type=\"log4net.Layout.PatternLayout\">");
+      tw.WriteLine("        <conversionPattern value=\"[%date] [%-7logger] [%-9thread] [%-5level] - %message%newline\" />");
+      tw.WriteLine("      </layout>");
+      tw.WriteLine("    </appender>");
+      tw.WriteLine(" ");
+      tw.WriteLine("    <root>");
+      tw.WriteLine("      <level value=\"ALL\" />");
+      tw.WriteLine("      <appender-ref ref=\"ConsoleAppender\" />");
+      tw.WriteLine("      <appender-ref ref=\"ErrorLossyFileAppender\" />");
+      tw.WriteLine("      <appender-ref ref=\"DefaultLogAppender\" />");
+      tw.WriteLine("    </root>");
+      tw.WriteLine("  </log4net>");
+      tw.WriteLine(" ");
+      tw.WriteLine("</configuration>");
+      tw.Flush();
+      tw.Close();
+    }
+
+    #endregion
+
+    #region Implementation of ICommonLogger
+
+    /// <summary>
+    /// Gets or sets the log level.
+    /// </summary>
+    /// <value>A <see cref="CommonLogLevel"/> value that indicates the minimum level messages must have to be 
+    /// written to the logger.</value>
+    public CommonLogLevel LogLevel
+    {
+      get { return MinLevel; }
+      set { MinLevel = value; }
+    }
+
+
+    /// <summary>
+    /// Writes a debug message to the Log  
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    public void Debug(CommonLogType logType, string format, params object[] args)
+    {
+      if (MinLevel >= CommonLogLevel.Debug)
+      {
+        _loggers[logType].Debug(FormatString(format, args));
+      }
+    }
+
+    /// <summary>
+    /// Writes an informational message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    public void Info(CommonLogType logType, string format, params object[] args)
+    {
+      if (MinLevel >= CommonLogLevel.Information)
+      {
+        _loggers[logType].Info(FormatString(format, args));
+      }
+    }
+
+    /// <summary>
+    /// Writes a warning message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    public void Warn(CommonLogType logType, string format, params object[] args)
+    {
+      if (MinLevel >= CommonLogLevel.Warning)
+      {
+        _loggers[logType].Warn(FormatString(format, args));
+      }
+    }
+
+    /// <summary>
+    /// Writes an error message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    public void Error(CommonLogType logType, string format, params object[] args)
+    {
+      _loggers[logType].Error(FormatString(format, args));
+    }
+
+    /// <summary>
+    /// Writes an Error <see cref="Exception"/> to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    public void Error(CommonLogType logType, Exception ex)
+    {
+      _loggers[logType].Error(FormatException(ex));
+    }
+
+    /// <summary>
+    /// Writes an error <see cref="Exception"/> to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="message">A message string.</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    public void Error(CommonLogType logType, string message, Exception ex)
+    {
+      Error(logType, message);
+      Error(logType, ex);
+    }
+
+    /// <summary>
+    /// Writes a critical error system message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    public void Critical(CommonLogType logType, string format, params object[] args)
+    {
+      if (MinLevel >= CommonLogLevel.Critical)
+      {
+        _loggers[logType].Fatal(FormatString(format, args));
+      }
+    }
+
+
+    /// <summary>
+    /// Writes an Critical error <see cref="Exception"/> to the  Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    public void Critical(CommonLogType logType, Exception ex)
+    {
+      if (MinLevel >= CommonLogLevel.Critical)
+      {
+        _loggers[logType].Fatal(FormatException(ex));
+      }
+    }
+
+    /// <summary>
+    /// Writes an critical <see cref="Exception"/> to the  Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="message">A message string.</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    public void Critical(CommonLogType logType, string message, Exception ex)
+    {
+      Critical(logType, message);
+      Critical(logType, ex);
+    }
+
+    #endregion
+
+    #region protected routines
+
+    /// <summary>
+    /// Writes an <see cref="Exception"/> instance.
+    /// </summary>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    protected string FormatException(Exception ex)
+    {
+      var sb = new StringBuilder();
+      sb.AppendFormat("Exception: " + SafeString(ex.ToString()));
+      sb.AppendFormat("  Message: " + SafeString(ex.Message));
+      sb.AppendFormat("  Site   : " + ex.TargetSite);
+      sb.AppendFormat("  Source : " + ex.Source);
+      if (ex.InnerException != null)
+      {
+        sb.AppendFormat("  Inner Exception(s):");
+        {
+          var stack = new Stack<Exception>();
+          stack.Push(ex);
+          while (stack.Count > 0)
+          {
+            var except = stack.Pop();
+            sb.AppendFormat("  -> " + except.Message);
+            if (except.InnerException != null)
+            {
+              stack.Push(except.InnerException);
+            }
+          }
+        }
+      }
+      sb.AppendFormat("  Stack Trace:");
+      sb.AppendFormat("  " + ex.StackTrace);
+      return sb.ToString();
+    }
+
+    /// <summary>
+    /// Replaces a password inside the string by stars
+    /// </summary>
+    /// <param name="logtext">String to replace</param>
+    /// <returns>String without password</returns>
+    protected string SafeString(String logtext)
+    {
+      return new Regex(@"Password=[^;]*;", RegexOptions.IgnoreCase).Replace(logtext, "Password=***;");
+    }
+
+    /// <summary>
+    /// Check for valid args and return formated string
+    /// </summary>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    /// <returns>formated string</returns>
+    protected string FormatString(string format, params object[] args)
+    {
+      var result = format;
+      if (args != null && args.Length > 0)
+      {
+        try 
+        {
+          result = string.Format(format, args);
+        }
+        catch {}
+      }
+      return result;
+    }
+
+    #endregion
+
+  }
+}
diff --git a/Common-MP-TVE3/Common.Utils/Logger/CommonLogLevel.cs b/Common-MP-TVE3/Common.Utils/Logger/CommonLogLevel.cs
new file mode 100644
index 0000000..c88baba
--- /dev/null
+++ b/Common-MP-TVE3/Common.Utils/Logger/CommonLogLevel.cs
@@ -0,0 +1,17 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MediaPortal.Common.Utils.Logger
+{
+  public enum CommonLogLevel
+  {
+    Critical,
+    Error,
+    Warning,
+    Information,
+    Debug,
+    All
+  }
+}
diff --git a/Common-MP-TVE3/Common.Utils/Logger/CommonLogType.cs b/Common-MP-TVE3/Common.Utils/Logger/CommonLogType.cs
new file mode 100644
index 0000000..8bf9c7f
--- /dev/null
+++ b/Common-MP-TVE3/Common.Utils/Logger/CommonLogType.cs
@@ -0,0 +1,22 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MediaPortal.Common.Utils.Logger
+{
+  public enum CommonLogType
+  {
+    Log =1,
+    Error = 2,
+    Config = 3 ,
+    Recorder = 100,
+    EPG =101,
+    VMR9 = 102,
+    MusicShareWatcher =103,
+    WebEPG = 104,
+    Tv =200,
+    TvConfig = 201,
+    TvPlugIn = 202,
+  }
+}
diff --git a/Common-MP-TVE3/Common.Utils/Logger/CommonLogger.cs b/Common-MP-TVE3/Common.Utils/Logger/CommonLogger.cs
new file mode 100644
index 0000000..3b16fb7
--- /dev/null
+++ b/Common-MP-TVE3/Common.Utils/Logger/CommonLogger.cs
@@ -0,0 +1,7 @@
+﻿namespace MediaPortal.Common.Utils.Logger
+{
+  public static class CommonLogger
+  {
+    public static ICommonLogger Instance { get; set; }
+  }
+}
diff --git a/Common-MP-TVE3/Common.Utils/Logger/ICommonLogger.cs b/Common-MP-TVE3/Common.Utils/Logger/ICommonLogger.cs
new file mode 100644
index 0000000..88428d3
--- /dev/null
+++ b/Common-MP-TVE3/Common.Utils/Logger/ICommonLogger.cs
@@ -0,0 +1,87 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MediaPortal.Common.Utils.Logger
+{
+  public interface ICommonLogger
+  {
+    /// <summary>
+    /// Gets or sets the log level.
+    /// </summary>
+    /// <value>A <see cref="CommonLogLevel"/> value that indicates the minimum level messages must have to be 
+    /// written to the logger.</value>
+    CommonLogLevel LogLevel { get; set; }
+
+    /// <summary>
+    /// Writes a debug message to the Log  
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    void Debug(CommonLogType logType, string format, params object[] args);
+
+    /// <summary>
+    /// Writes an informational message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    void Info(CommonLogType logType, string format, params object[] args);
+
+    /// <summary>
+    /// Writes a warning message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    void Warn(CommonLogType logType, string format, params object[] args);
+
+    /// <summary>
+    /// Writes an error message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    void Error(CommonLogType logType, string format, params object[] args);
+
+    /// <summary>
+    /// Writes an Error <see cref="Exception"/> to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    void Error(CommonLogType logType, Exception ex);
+
+    /// <summary>
+    /// Writes an error <see cref="Exception"/> to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="message">A message string.</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    void Error(CommonLogType logType, string message, Exception ex);
+
+    /// <summary>
+    /// Writes a critical error system message to the Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="format">A composite format string.</param>
+    /// <param name="args">An array of objects to write using format.</param>
+    void Critical(CommonLogType logType, string format, params object[] args);
+
+    /// <summary>
+    /// Writes an Critical error <see cref="Exception"/> to the  Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    void Critical(CommonLogType logType, Exception ex);
+
+    /// <summary>
+    /// Writes an critical <see cref="Exception"/> to the  Log
+    /// </summary>
+    /// <param name="logType">Defines log4net logger</param>
+    /// <param name="message">A message string.</param>
+    /// <param name="ex">The <see cref="Exception"/> to write.</param>
+    void Critical(CommonLogType logType, string message, Exception ex);
+  }
+}
diff --git a/TvEngine3/TVLibrary/TVServer.Base/log4net.config b/TvEngine3/TVLibrary/TVServer.Base/log4net.config
index 7fd122a..f7de181 100644
--- a/TvEngine3/TVLibrary/TVServer.Base/log4net.config
+++ b/TvEngine3/TVLibrary/TVServer.Base/log4net.config
@@ -6,31 +6,31 @@
 
   <log4net>
     <appender name="DefaultLogAppender" type="log4net.Appender.RollingFileAppender">
-      <file value="tv.log" />
+      <file value="[Name].log" />
       <appendToFile value="true" />
       <rollingStyle value="Once" />
-      <maxSizeRollBackups value="4" />
+      <maxSizeRollBackups value="5" />
       <maximumFileSize value="1MB" />
       <staticLogFileName value="true" />
       <layout type="log4net.Layout.PatternLayout">
-        <conversionPattern value="[%date] [%-7timestamp] [%-9thread] [%-5level] - %message%newline" />
+        <conversionPattern value="[%date] [%-7logger] [%-9thread] [%-5level] - %message%newline" />
       </layout>
     </appender>
 
     <appender name="ErrorLogAppender" type="log4net.Appender.RollingFileAppender">
-      <file value="Error.log" />
+      <file value="[Name]-Error.log" />
       <appendToFile value="true" />
       <rollingStyle value="Once" />
-      <maxSizeRollBackups value="4" />
+      <maxSizeRollBackups value="5" />
       <maximumFileSize value="1MB" />
       <staticLogFileName value="true" />
       <layout type="log4net.Layout.PatternLayout">
-        <conversionPattern value="[%date] [%-7timestamp] [%-9thread] [%-5level] - %message%newline" />
+        <conversionPattern value="[%date] [%-7logger] [%-9thread] [%-5level] - %message%newline" />
       </layout>
     </appender>
 
     <appender name="ErrorLossyFileAppender" type="log4net.Appender.BufferingForwardingAppender">
-      <bufferSize value="25" />
+      <bufferSize value="1" />
       <lossy value="true"/>
       <evaluator type="log4net.Core.LevelEvaluator">
         <threshold value="ERROR" />
@@ -40,7 +40,7 @@
 
     <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
       <layout type="log4net.Layout.PatternLayout">
-        <conversionPattern value="[%-7timestamp] [%-9thread] [%-5level] - %message%newline" />
+        <conversionPattern value="[%date] [%-9thread] [%-5level] - %message%newline" />
       </layout>
     </appender>
 
diff --git a/TvEngine3/TVLibrary/TvLibrary.Interfaces/Logging/Log.cs b/TvEngine3/TVLibrary/TvLibrary.Interfaces/Logging/Log.cs
index 8267ebf..a680cca 100644
--- a/TvEngine3/TVLibrary/TvLibrary.Interfaces/Logging/Log.cs
+++ b/TvEngine3/TVLibrary/TvLibrary.Interfaces/Logging/Log.cs
@@ -24,6 +24,7 @@ using System.IO;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
+using MediaPortal.Common.Utils.Logger;
 
 namespace TvLibrary.Log
 {
@@ -406,7 +407,6 @@ namespace TvLibrary.Log
       {
         try
         {
-          string logFileName = GetFileName(logType);
           string logLine = string.Format(format, arg);
 
           if (IsRepetition(logLine))
@@ -415,6 +415,18 @@ namespace TvLibrary.Log
           }
           CacheLogLine(logLine);
 
+          // implementation
+          switch (logType)
+          {
+            case LogType.Debug: CommonLogger.Instance.Debug(CommonLogType.Log, format, arg); break;
+            case LogType.Info: CommonLogger.Instance.Info(CommonLogType.Log, format, arg); break;
+            case LogType.Error:CommonLogger.Instance.Error(CommonLogType.Log, format, arg); break;
+            case LogType.Epg: CommonLogger.Instance.Info(CommonLogType.EPG, format, arg); break;
+          }
+          
+
+          /*
+          string logFileName = GetFileName(logType);
           if (CheckLogPrepared(logFileName))
           {
             using (StreamWriter writer = new StreamWriter(logFileName, true, Encoding.UTF8))
@@ -428,6 +440,7 @@ namespace TvLibrary.Log
               writer.Close();
             }
           }
+           */
         }
         catch (Exception) {}
       }
diff --git a/TvEngine3/TVLibrary/TvLibrary.Interfaces/TvLibrary.Interfaces.csproj b/TvEngine3/TVLibrary/TvLibrary.Interfaces/TvLibrary.Interfaces.csproj
index d63583b..e67890a 100644
--- a/TvEngine3/TVLibrary/TvLibrary.Interfaces/TvLibrary.Interfaces.csproj
+++ b/TvEngine3/TVLibrary/TvLibrary.Interfaces/TvLibrary.Interfaces.csproj
@@ -136,6 +136,10 @@
     <Compile Include="TvException.cs" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\..\..\Common-MP-TVE3\Common.Utils\Common.Utils.csproj">
+      <Project>{F6EDA1F3-3DCD-43F4-8A90-E32DA70C8227}</Project>
+      <Name>Common.Utils</Name>
+    </ProjectReference>
     <ProjectReference Include="..\..\..\Common-MP-TVE3\DirectShowLib\DirectShowLib.csproj">
       <Project>{CE009BFA-5EF6-4153-B2A1-2FA79698A9AE}</Project>
       <Name>DirectShowLib</Name>
diff --git a/TvEngine3/TVLibrary/TvService/Service1.cs b/TvEngine3/TVLibrary/TvService/Service1.cs
index 5a55649..5e70386 100644
--- a/TvEngine3/TVLibrary/TvService/Service1.cs
+++ b/TvEngine3/TVLibrary/TvService/Service1.cs
@@ -25,6 +25,7 @@ using System.Collections.Specialized;
 using System.Configuration;
 using System.Configuration.Install;
 using System.Diagnostics;
+using System.IO;
 using System.Security.Principal;
 using System.ServiceProcess;
 using System.Threading;
@@ -32,6 +33,7 @@ using System.Windows.Forms;
 using System.Runtime.CompilerServices;
 using System.Runtime.Remoting;
 using System.Xml;
+using MediaPortal.Common.Utils.Logger;
 using TvDatabase;
 using TvLibrary.Log;
 using TvControl;
@@ -91,6 +93,16 @@ namespace TvService
     /// </summary>
     private static void Main(string[] args)
     {
+      // Init Common logger -> this will enable TVPlugin to write in the Mediaportal.log file
+      var loggerName = Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]);
+      var dataPath = Log.GetPathName();
+      var loggerPath = Path.Combine(dataPath, "\\log");
+#if DEBUG
+      if (loggerName != null) loggerName = loggerName.Replace(".vshost", "");
+#endif
+      CommonLogger.Instance = new CommonLog4NetLogger(loggerName, dataPath, loggerPath);
+
+      
       NameValueCollection appSettings = ConfigurationManager.AppSettings;
       appSettings.Set("GentleConfigFile", String.Format(@"{0}\gentle.config", PathManager.GetDataPath));
 
diff --git a/mediaportal/MediaPortal.Base/log4net.config b/mediaportal/MediaPortal.Base/log4net.config
index a2378d1..f7de181 100644
--- a/mediaportal/MediaPortal.Base/log4net.config
+++ b/mediaportal/MediaPortal.Base/log4net.config
@@ -6,31 +6,31 @@
 
   <log4net>
     <appender name="DefaultLogAppender" type="log4net.Appender.RollingFileAppender">
-      <file value="MediaPortal.log" />
+      <file value="[Name].log" />
       <appendToFile value="true" />
       <rollingStyle value="Once" />
-      <maxSizeRollBackups value="4" />
+      <maxSizeRollBackups value="5" />
       <maximumFileSize value="1MB" />
       <staticLogFileName value="true" />
       <layout type="log4net.Layout.PatternLayout">
-        <conversionPattern value="[%date] [%-7timestamp] [%-9thread] [%-5level] - %message%newline" />
+        <conversionPattern value="[%date] [%-7logger] [%-9thread] [%-5level] - %message%newline" />
       </layout>
     </appender>
 
     <appender name="ErrorLogAppender" type="log4net.Appender.RollingFileAppender">
-      <file value="Error.log" />
+      <file value="[Name]-Error.log" />
       <appendToFile value="true" />
       <rollingStyle value="Once" />
-      <maxSizeRollBackups value="4" />
+      <maxSizeRollBackups value="5" />
       <maximumFileSize value="1MB" />
       <staticLogFileName value="true" />
       <layout type="log4net.Layout.PatternLayout">
-        <conversionPattern value="[%date] [%-7timestamp] [%-9thread] [%-5level] - %message%newline" />
+        <conversionPattern value="[%date] [%-7logger] [%-9thread] [%-5level] - %message%newline" />
       </layout>
     </appender>
 
     <appender name="ErrorLossyFileAppender" type="log4net.Appender.BufferingForwardingAppender">
-      <bufferSize value="25" />
+      <bufferSize value="1" />
       <lossy value="true"/>
       <evaluator type="log4net.Core.LevelEvaluator">
         <threshold value="ERROR" />
@@ -40,7 +40,7 @@
 
     <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
       <layout type="log4net.Layout.PatternLayout">
-        <conversionPattern value="[%-7timestamp] [%-9thread] [%-5level] - %message%newline" />
+        <conversionPattern value="[%date] [%-9thread] [%-5level] - %message%newline" />
       </layout>
     </appender>
 
diff --git a/mediaportal/Utils/ServiceImplementations/log4netLogger.cs b/mediaportal/Utils/ServiceImplementations/log4netLogger.cs
deleted file mode 100644
index 9578dbc..0000000
--- a/mediaportal/Utils/ServiceImplementations/log4netLogger.cs
+++ /dev/null
@@ -1,172 +0,0 @@
-﻿using System;
-using System.IO;
-using System.Windows.Forms;
-using System.Xml;
-using log4net;
-using MediaPortal.Configuration;
-using MediaPortal.Profile;
-using MediaPortal.Services;
-
-namespace MediaPortal.ServiceImplementations
-{
-  public class Log4netLogger : MediaPortal.Services.ILog
-  {
-    #region Variables
-    private Level _minLevel;
-    private bool _configuration;
-    #endregion
-
-    #region Constructors/Destructors
-    public Log4netLogger()
-    {
-      string logPath = Config.GetFolder(Config.Dir.Log);
-      using (Settings xmlreader = new MPSettings())
-      {
-        _minLevel = (Level)Enum.Parse(typeof(Level), xmlreader.GetValueAsString("general", "loglevel", "2"));
-      }
-      _configuration = false;
-      string appPath = Path.GetDirectoryName(Application.ExecutablePath);
-      
-      XmlDocument xmlDoc = new XmlDocument();
-      FileStream fs = new FileStream(Path.Combine(appPath, "log4net.config"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
-      xmlDoc.Load(fs);
-      fs.Close();
-      
-      XmlNodeList nodeList = xmlDoc.SelectNodes("configuration/log4net/appender/file");
-      foreach (XmlNode node in nodeList)
-      {
-        if (node.Attributes != null)
-        {
-          foreach (XmlAttribute attribute in node.Attributes)
-          {
-            if (attribute.Name.Equals("value"))
-            {
-              attribute.Value = Path.Combine(logPath, Path.GetFileName(attribute.Value));
-              break;
-            }
-          }
-        }
-      }
-      MemoryStream mStream = new MemoryStream();
-      xmlDoc.Save(mStream);
-      mStream.Seek(0, SeekOrigin.Begin);
-      log4net.Config.XmlConfigurator.Configure(mStream);
-    }
-    #endregion
-
-    private string GetLogTypeName(LogType type)
-    {
-      if (_configuration) return "Config";
-      switch (type)
-      {
-        case LogType.Recorder: return ("Rec");
-        case LogType.Error: return ("Error");
-        case LogType.EPG: return ("EPG");
-        case LogType.VMR9:return ("VMR9");
-        case LogType.MusicShareWatcher:return ("MusicSh");
-        case LogType.WebEPG:return ("WebEPG");
-        default: return ("Log");
-      }     
-    }
-
-
-    #region Implementation of ILog
-    public void BackupLogFiles() {}
-    public void BackupLogFile(LogType logType) {}
-
-    
-    public void Info(string format, params object[] args)
-    {
-      Info(LogType.Log, format, args);
-    }
-
-    public void Info(LogType type, string format, params object[] args)
-    {
-      if (Level.Information <= _minLevel)
-      {
-        if (args.Length > 0)
-        {
-          LogManager.GetLogger(GetLogTypeName(type)).InfoFormat(format, args);
-        }
-        else
-        {
-          LogManager.GetLogger(GetLogTypeName(type)).Info(format);
-        }
-      }
-    }
-
-    public void Warn(string format, params object[] args)
-    {
-      Warn(LogType.Log, format, args);
-    }
-
-    public void Warn(LogType type, string format, params object[] args)
-    {
-      if (Level.Warning <= _minLevel)
-      {
-        if (args.Length > 0)
-        {
-          LogManager.GetLogger(GetLogTypeName(type)).WarnFormat(format, args);
-        }
-        else
-        {
-          LogManager.GetLogger(GetLogTypeName(type)).Warn(format);
-        }
-      }
-    }
-
-    public void Debug(string format, params object[] args)
-    {
-      Debug(LogType.Log, format, args);
-    }
-
-    public void Debug(LogType type, string format, params object[] args)
-    {
-      if (Level.Debug <= _minLevel)
-      {
-        if (args.Length > 0)
-        {
-          LogManager.GetLogger(GetLogTypeName(type)).DebugFormat(format, args);
-        }
-        else
-        {
-          LogManager.GetLogger(GetLogTypeName(type)).Debug(format);
-        }
-      }
-    }
-
-    public void Error(string format, params object[] args)
-    {
-      Error(LogType.Error, format, args);
-    }
-
-    public void Error(LogType type, string format, params object[] args)
-    {
-      if (args.Length > 0)
-      {
-        LogManager.GetLogger(GetLogTypeName(type)).ErrorFormat(format, args);
-      }
-      else
-      {
-        LogManager.GetLogger(GetLogTypeName(type)).Error(format);
-      }
-    }
-
-    public void Error(Exception ex)
-    {
-      LogManager.GetLogger(LogType.Log.GetType()).Error("", ex);
-    }
-
-    public void SetConfigurationMode()
-    {
-      _configuration = true;
-    }
-
-    public void SetLogLevel(Level logLevel)
-    {
-      _minLevel = logLevel;
-    }
-
-    #endregion
-  }
-}
diff --git a/mediaportal/Utils/Services/GlobalServiceProvider.cs b/mediaportal/Utils/Services/GlobalServiceProvider.cs
index a0646c4..7fb8782 100644
--- a/mediaportal/Utils/Services/GlobalServiceProvider.cs
+++ b/mediaportal/Utils/Services/GlobalServiceProvider.cs
@@ -18,10 +18,12 @@
 
 #endregion
 
+using System;
 using System.IO;
 using System.Windows.Forms;
 using MediaPortal.ServiceImplementations;
 using MediaPortal.Threading;
+using MediaPortal.Common.Utils.Logger;
 
 namespace MediaPortal.Services
 {
@@ -112,7 +114,18 @@ namespace MediaPortal.Services
 
     private static ILog LogServiceRequested(ServiceProvider services)
     {
-      ILog log = new LogImpl();
+      // Init Common logger -> this will enable TVPlugin to write in the Mediaportal.log file
+      var loggerName = Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]);
+      var dataPath = Configuration.Config.GetFolder(Configuration.Config.Dir.Config);
+      var loggerPath = Configuration.Config.GetFolder(Configuration.Config.Dir.Log);
+#if DEBUG
+      if (loggerName != null) loggerName = loggerName.Replace(".vshost", "");
+#endif
+      CommonLogger.Instance = new CommonLog4NetLogger(loggerName, dataPath, loggerPath);
+      
+      // Init wrapper from CommonLogger to the current ILog interface
+      // -> this will ensure that all external plugins will work without recompile
+      ILog log = new Log4NetWrapper();  //LogImpl();
       services.Add<ILog>(log);
       return log;
 
diff --git a/mediaportal/Utils/Utils.csproj b/mediaportal/Utils/Utils.csproj
index 1be5e50..51ecd22 100644
--- a/mediaportal/Utils/Utils.csproj
+++ b/mediaportal/Utils/Utils.csproj
@@ -149,7 +149,7 @@
     <Compile Include="Localisation\StringsFile\StringSection.cs" />
     <Compile Include="Localisation\StringsFile\StringFile.cs" />
     <Compile Include="ServiceImplementations\Log.cs" />
-    <Compile Include="ServiceImplementations\log4netLogger.cs" />
+    <Compile Include="ServiceImplementations\Log4NetWrapper.cs" />
     <Compile Include="ServiceImplementations\LogImpl.cs" />
     <Compile Include="Services\GlobalServiceProvider.cs" />
     <Compile Include="Services\ILog.cs" />
@@ -219,6 +219,12 @@
       <Isolated>False</Isolated>
     </COMReference>
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\..\Common-MP-TVE3\Common.Utils\Common.Utils.csproj">
+      <Project>{F6EDA1F3-3DCD-43F4-8A90-E32DA70C8227}</Project>
+      <Name>Common.Utils</Name>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
     <PreBuildEvent>
diff --git a/mediaportal/WatchDog/PostTestActions.cs b/mediaportal/WatchDog/PostTestActions.cs
index 6d44f51..a7d4505 100644
--- a/mediaportal/WatchDog/PostTestActions.cs
+++ b/mediaportal/WatchDog/PostTestActions.cs
@@ -78,7 +78,7 @@ namespace WatchDog
         sw.WriteLine("<ul>");
         sw.WriteLine("<li><a href=MediaPortal.log>MediaPortal.log</a></li>");
         sw.WriteLine("<li><a href=Configuration.log>Configuration.log</a></li>");
-        sw.WriteLine("<li><a href=Error.log>Error.log</a></li>");
+        sw.WriteLine("<li><a href=MediaPortal-Error.log>MediaPortal-Error.log</a></li>");
         sw.WriteLine("<li><a href=Recorder.log>Recorder.log</a></li>");
         if (File.Exists(_tmpDir + "\\TsReader.log"))
         {
@@ -93,7 +93,7 @@ namespace WatchDog
           sw.WriteLine("<ul>");
           sw.WriteLine("<li><a href=tvserver_tv.log>TV.log</a></li>");
           sw.WriteLine("<li><a href=tvserver_epg.log>EPG.log</a></li>");
-          sw.WriteLine("<li><a href=tvserver_error.log>Error.log</a></li>");
+          sw.WriteLine("<li><a href=tvserver_error.log>TV-Error.log</a></li>");
           sw.WriteLine("<li><a href=tvserver_Streaming Server.log>Streaming Server.log</a></li>");
           sw.WriteLine("<li><a href=tvserver_TsWriter.log>TsWriter.log</a></li>");
           sw.WriteLine("<li><a href=tvserver_Player.log>Player.log</a></li>");
-- 
1.7.10.msysgit.1

