Fixed some project fragmentation that came from efforts to go portable

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti
2012-07-30 23:38:00 -04:00
parent 7d48e20aea
commit d4c75e3974
20 changed files with 47 additions and 152 deletions

View File

@@ -0,0 +1,19 @@
using MediaBrowser.Common.Logging;
namespace MediaBrowser.Common.Configuration
{
/// <summary>
/// Serves as a common base class for the Server and UI application Configurations
/// </summary>
public class BaseApplicationConfiguration
{
public LogSeverity LogSeverity { get; set; }
public int HttpServerPortNumber { get; set; }
public BaseApplicationConfiguration()
{
LogSeverity = LogSeverity.Info;
HttpServerPortNumber = 8096;
}
}
}

View File

@@ -7,15 +7,11 @@ namespace MediaBrowser.Common.Json
{
public static void SerializeToStream<T>(T obj, Stream stream)
{
Configure();
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
}
public static void SerializeToFile<T>(T obj, string file)
{
Configure();
using (StreamWriter streamWriter = new StreamWriter(file))
{
ServiceStack.Text.JsonSerializer.SerializeToWriter<T>(obj, streamWriter);
@@ -24,8 +20,6 @@ namespace MediaBrowser.Common.Json
public static object DeserializeFromFile(Type type, string file)
{
Configure();
using (Stream stream = File.OpenRead(file))
{
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
@@ -34,8 +28,6 @@ namespace MediaBrowser.Common.Json
public static T DeserializeFromFile<T>(string file)
{
Configure();
using (Stream stream = File.OpenRead(file))
{
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
@@ -44,19 +36,15 @@ namespace MediaBrowser.Common.Json
public static T DeserializeFromStream<T>(Stream stream)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
}
public static T DeserializeFromString<T>(string data)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(data);
}
private static void Configure()
public static void Configure()
{
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
ServiceStack.Text.JsConfig.IncludeNullValues = false;

View File

@@ -6,12 +6,12 @@ using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using MediaBrowser.Common.Json;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Logging;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Progress;
using MediaBrowser.Common.Json;
namespace MediaBrowser.Common.Kernel
{
@@ -93,19 +93,21 @@ namespace MediaBrowser.Common.Kernel
public virtual void Init(IProgress<TaskProgress> progress)
{
JsonSerializer.Configure();
ReloadLogger();
ReloadConfiguration();
ReloadHttpServer();
ReloadComposableParts();
}
private void ReloadLogger()
{
DisposeLogger();
if (!Directory.Exists(LogDirectoryPath))
{
Directory.CreateDirectory(LogDirectoryPath);
@@ -115,8 +117,8 @@ namespace MediaBrowser.Common.Kernel
LogFilePath = Path.Combine(LogDirectoryPath, now.ToString("dMyyyy") + "-" + now.Ticks + ".log");
FileStream fs = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
FileStream fs = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
Logger.LoggerInstance = new StreamLogger(fs);
}

View File

@@ -0,0 +1,76 @@
using System;
using System.Text;
using System.Threading;
namespace MediaBrowser.Common.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);
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Text;
namespace MediaBrowser.Common.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] ");
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace MediaBrowser.Common.Logging
{
[Flags]
public enum LogSeverity
{
None = 0,
Debug = 1,
Info = 2,
Warning = 4,
Error = 8
}
}

View File

@@ -0,0 +1,34 @@
using System;
namespace MediaBrowser.Common.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);
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.IO;
using System.Text;
namespace MediaBrowser.Common.Logging
{
/// <summary>
/// Provides a Logger that can write to any Stream
/// </summary>
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();
}
}
}

View File

@@ -48,10 +48,16 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration\BaseApplicationConfiguration.cs" />
<Compile Include="Events\GenericItemEventArgs.cs" />
<Compile Include="Json\JsonSerializer.cs" />
<Compile Include="Kernel\BaseKernel.cs" />
<Compile Include="Kernel\KernelContext.cs" />
<Compile Include="Logging\BaseLogger.cs" />
<Compile Include="Logging\Logger.cs" />
<Compile Include="Logging\LogRow.cs" />
<Compile Include="Logging\LogSeverity.cs" />
<Compile Include="Logging\StreamLogger.cs" />
<Compile Include="Net\CollectionExtensions.cs" />
<Compile Include="Net\Handlers\BaseEmbeddedResourceHandler.cs" />
<Compile Include="Net\Handlers\BaseHandler.cs" />
@@ -67,10 +73,6 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MediaBrowser.Logging\MediaBrowser.Logging.csproj">
<Project>{37032b77-fe2e-4ec5-b7e4-baf634443578}</Project>
<Name>MediaBrowser.Logging</Name>
</ProjectReference>
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">
<Project>{7eeeb4bb-f3e8-48fc-b4c5-70f0fff8329b}</Project>
<Name>MediaBrowser.Model</Name>

View File

@@ -1,8 +1,8 @@
using System;
using System.IO;
using MediaBrowser.Common.Json;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Common.Json;
namespace MediaBrowser.Common.Plugins
{
@@ -100,19 +100,6 @@ namespace MediaBrowser.Common.Plugins
}
}
public void ReloadConfiguration()
{
if (!File.Exists(ConfigurationPath))
{
Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
}
else
{
Configuration = JsonSerializer.DeserializeFromFile(ConfigurationType, ConfigurationPath) as BasePluginConfiguration;
Configuration.DateLastModified = File.GetLastWriteTime(ConfigurationPath);
}
}
/// <summary>
/// Starts the plugin.
/// </summary>
@@ -126,5 +113,18 @@ namespace MediaBrowser.Common.Plugins
public virtual void Dispose()
{
}
public void ReloadConfiguration()
{
if (!File.Exists(ConfigurationPath))
{
Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
}
else
{
Configuration = JsonSerializer.DeserializeFromFile(ConfigurationType, ConfigurationPath) as BasePluginConfiguration;
Configuration.DateLastModified = File.GetLastWriteTime(ConfigurationPath);
}
}
}
}