aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Logging
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Logging')
-rw-r--r--MediaBrowser.Logging/BaseLogger.cs76
-rw-r--r--MediaBrowser.Logging/LogRow.cs40
-rw-r--r--MediaBrowser.Logging/LogSeverity.cs14
-rw-r--r--MediaBrowser.Logging/Logger.cs34
-rw-r--r--MediaBrowser.Logging/MediaBrowser.Logging.csproj53
-rw-r--r--MediaBrowser.Logging/Properties/AssemblyInfo.cs30
-rw-r--r--MediaBrowser.Logging/StreamLogger.cs32
7 files changed, 279 insertions, 0 deletions
diff --git a/MediaBrowser.Logging/BaseLogger.cs b/MediaBrowser.Logging/BaseLogger.cs
new file mode 100644
index 0000000000..d73b473930
--- /dev/null
+++ b/MediaBrowser.Logging/BaseLogger.cs
@@ -0,0 +1,76 @@
+using System;
+using System.Text;
+using System.Threading;
+
+namespace MediaBrowser.Logging
+{
+ public abstract class BaseLogger : IDisposable
+ {
+ public LogSeverity LogSeverity { get; set; }
+
+ public void LogInfo(string message, params object[] paramList)
+ {
+ LogEntry(message, LogSeverity.Info, paramList);
+ }
+
+ public void LogDebugInfo(string message, params object[] paramList)
+ {
+ LogEntry(message, LogSeverity.Debug, paramList);
+ }
+
+ public void LogError(string message, params object[] paramList)
+ {
+ LogEntry(message, LogSeverity.Error, paramList);
+ }
+
+ public void LogException(string message, Exception exception, params object[] paramList)
+ {
+ StringBuilder builder = new StringBuilder();
+
+ if (exception != null)
+ {
+ builder.AppendFormat("Exception. Type={0} Msg={1} StackTrace={3}{2}",
+ exception.GetType().FullName,
+ exception.Message,
+ exception.StackTrace,
+ Environment.NewLine);
+ }
+
+ message = string.Format(message, paramList);
+
+ LogError(string.Format("{0} ( {1} )", message, builder));
+ }
+
+ public void LogWarning(string message, params object[] paramList)
+ {
+ LogEntry(message, LogSeverity.Warning, paramList);
+ }
+
+ private void LogEntry(string message, LogSeverity severity, params object[] paramList)
+ {
+ if (severity < LogSeverity) return;
+
+ message = string.Format(message, paramList);
+
+ Thread currentThread = Thread.CurrentThread;
+
+ LogRow row = new LogRow()
+ {
+ Severity = severity,
+ Message = message,
+ Category = string.Empty,
+ ThreadId = currentThread.ManagedThreadId,
+ //ThreadName = currentThread.Name,
+ Time = DateTime.Now
+ };
+
+ LogEntry(row);
+ }
+
+ public virtual void Dispose()
+ {
+ }
+
+ protected abstract void LogEntry(LogRow row);
+ }
+}
diff --git a/MediaBrowser.Logging/LogRow.cs b/MediaBrowser.Logging/LogRow.cs
new file mode 100644
index 0000000000..da699035a2
--- /dev/null
+++ b/MediaBrowser.Logging/LogRow.cs
@@ -0,0 +1,40 @@
+using System;
+using System.Text;
+
+namespace MediaBrowser.Logging
+{
+ public struct LogRow
+ {
+ const string TimePattern = "h:mm:ss.fff tt d/M/yyyy";
+
+ public LogSeverity Severity { get; set; }
+ public string Message { get; set; }
+ public string Category { get; set; }
+ public int ThreadId { get; set; }
+ public string ThreadName { get; set; }
+ public DateTime Time { get; set; }
+
+ public override string ToString()
+ {
+ StringBuilder builder = new StringBuilder();
+ builder.Append(Time.ToString(TimePattern))
+ .Append(" , ")
+ .Append(Enum.GetName(typeof(LogSeverity), Severity))
+ .Append(" , ")
+ .Append(Encode(Message))
+ .Append(" , ")
+ .Append(Encode(Category))
+ .Append(" , ")
+ .Append(ThreadId)
+ .Append(" , ")
+ .Append(Encode(ThreadName));
+
+ return builder.ToString();
+ }
+
+ private string Encode(string str)
+ {
+ return (str ?? "").Replace(",", ",,").Replace(Environment.NewLine, " [n] ");
+ }
+ }
+}
diff --git a/MediaBrowser.Logging/LogSeverity.cs b/MediaBrowser.Logging/LogSeverity.cs
new file mode 100644
index 0000000000..70c4f6304b
--- /dev/null
+++ b/MediaBrowser.Logging/LogSeverity.cs
@@ -0,0 +1,14 @@
+using System;
+
+namespace MediaBrowser.Logging
+{
+ [Flags]
+ public enum LogSeverity
+ {
+ None = 0,
+ Debug = 1,
+ Info = 2,
+ Warning = 4,
+ Error = 8
+ }
+}
diff --git a/MediaBrowser.Logging/Logger.cs b/MediaBrowser.Logging/Logger.cs
new file mode 100644
index 0000000000..db46010a41
--- /dev/null
+++ b/MediaBrowser.Logging/Logger.cs
@@ -0,0 +1,34 @@
+using System;
+
+namespace MediaBrowser.Logging
+{
+ public static class Logger
+ {
+ public static BaseLogger LoggerInstance { get; set; }
+
+ public static void LogInfo(string message, params object[] paramList)
+ {
+ LoggerInstance.LogInfo(message, paramList);
+ }
+
+ public static void LogDebugInfo(string message, params object[] paramList)
+ {
+ LoggerInstance.LogDebugInfo(message, paramList);
+ }
+
+ public static void LogError(string message, params object[] paramList)
+ {
+ LoggerInstance.LogError(message, paramList);
+ }
+
+ public static void LogException(string message, Exception ex, params object[] paramList)
+ {
+ LoggerInstance.LogException(message, ex, paramList);
+ }
+
+ public static void LogWarning(string message, params object[] paramList)
+ {
+ LoggerInstance.LogWarning(message, paramList);
+ }
+ }
+}
diff --git a/MediaBrowser.Logging/MediaBrowser.Logging.csproj b/MediaBrowser.Logging/MediaBrowser.Logging.csproj
new file mode 100644
index 0000000000..39840f71fd
--- /dev/null
+++ b/MediaBrowser.Logging/MediaBrowser.Logging.csproj
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{37032B77-FE2E-4EC5-B7E4-BAF634443578}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>MediaBrowser.Logging</RootNamespace>
+ <AssemblyName>MediaBrowser.Logging</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkProfile>Profile95</TargetFrameworkProfile>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <!-- A reference to the entire .NET Framework is automatically included -->
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="BaseLogger.cs" />
+ <Compile Include="Logger.cs" />
+ <Compile Include="LogRow.cs" />
+ <Compile Include="LogSeverity.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="StreamLogger.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ No newline at end of file
diff --git a/MediaBrowser.Logging/Properties/AssemblyInfo.cs b/MediaBrowser.Logging/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000000..fa0d3817d2
--- /dev/null
+++ b/MediaBrowser.Logging/Properties/AssemblyInfo.cs
@@ -0,0 +1,30 @@
+using System.Resources;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("MediaBrowser.Logging")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("MediaBrowser.Logging")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+[assembly: NeutralResourcesLanguage("en")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/MediaBrowser.Logging/StreamLogger.cs b/MediaBrowser.Logging/StreamLogger.cs
new file mode 100644
index 0000000000..f1a16c7ad1
--- /dev/null
+++ b/MediaBrowser.Logging/StreamLogger.cs
@@ -0,0 +1,32 @@
+using System;
+using System.IO;
+using System.Text;
+
+namespace MediaBrowser.Logging
+{
+ public class StreamLogger : BaseLogger
+ {
+ private Stream Stream { get; set; }
+
+ public StreamLogger(Stream stream)
+ : base()
+ {
+ Stream = stream;
+ }
+
+ protected override void LogEntry(LogRow row)
+ {
+ byte[] bytes = new UTF8Encoding().GetBytes(row.ToString() + Environment.NewLine);
+
+ Stream.Write(bytes, 0, bytes.Length);
+ Stream.Flush();
+ }
+
+ public override void Dispose()
+ {
+ base.Dispose();
+
+ Stream.Dispose();
+ }
+ }
+}