add console logging during startup

This commit is contained in:
Luke Pulverenti 2014-01-08 23:44:51 -05:00
parent dcc5f9ebab
commit 247a40fa61
12 changed files with 79 additions and 50 deletions

View File

@ -187,7 +187,7 @@ namespace MediaBrowser.Common.Implementations.Logging
LogFilePath = Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Round(DateTime.Now.Ticks / 10000000) + ".log"); LogFilePath = Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Round(DateTime.Now.Ticks / 10000000) + ".log");
Directory.CreateDirectory(Path.GetDirectoryName(LogFilePath)); Directory.CreateDirectory(Path.GetDirectoryName(LogFilePath));
AddFileTarget(LogFilePath, level); AddFileTarget(LogFilePath, level);
LogSeverity = level; LogSeverity = level;
@ -212,5 +212,26 @@ namespace MediaBrowser.Common.Implementations.Logging
{ {
LogManager.Flush(); LogManager.Flush();
} }
public void AddConsoleOutput()
{
var target = new ConsoleTarget()
{
Layout = "${level}, ${logger}, ${message}",
Error = false
};
RemoveTarget("ConsoleTarget");
target.Name = "ConsoleTarget";
AddLogTarget(target, LogSeverity);
}
public void RemoveConsoleOutput()
{
RemoveTarget("ConsoleTarget");
}
} }
} }

View File

@ -25,9 +25,9 @@ namespace MediaBrowser.Common.Net
/// <summary> /// <summary>
/// Starts this instance. /// Starts this instance.
/// </summary> /// </summary>
/// <param name="urlPrefix">The URL prefix.</param> /// <param name="urlPrefixes">The URL prefixes.</param>
/// <param name="enableHttpLogging">if set to <c>true</c> [enable HTTP logging].</param> /// <param name="enableHttpLogging">if set to <c>true</c> [enable HTTP logging].</param>
void Start(string urlPrefix, bool enableHttpLogging); void Start(IEnumerable<string> urlPrefixes, bool enableHttpLogging);
/// <summary> /// <summary>
/// Starts the web socket server. /// Starts the web socket server.

View File

@ -20,12 +20,6 @@ namespace MediaBrowser.Controller
/// <value>The name of the web application.</value> /// <value>The name of the web application.</value>
string WebApplicationName { get; } string WebApplicationName { get; }
/// <summary>
/// Gets the HTTP server URL prefix.
/// </summary>
/// <value>The HTTP server URL prefix.</value>
string HttpServerUrlPrefix { get; }
/// <summary> /// <summary>
/// Gets a value indicating whether [supports automatic run at startup]. /// Gets a value indicating whether [supports automatic run at startup].
/// </summary> /// </summary>

View File

@ -13,13 +13,13 @@ namespace MediaBrowser.Controller.Net
/// Gets the URL prefix. /// Gets the URL prefix.
/// </summary> /// </summary>
/// <value>The URL prefix.</value> /// <value>The URL prefix.</value>
string UrlPrefix { get; } IEnumerable<string> UrlPrefixes { get; }
/// <summary> /// <summary>
/// Starts the specified server name. /// Starts the specified server name.
/// </summary> /// </summary>
/// <param name="urlPrefix">The URL.</param> /// <param name="urlPrefixes">The URL prefixes.</param>
void StartServer(string urlPrefix); void StartServer(IEnumerable<string> urlPrefixes);
/// <summary> /// <summary>
/// Gets a value indicating whether [supports web sockets]. /// Gets a value indicating whether [supports web sockets].

View File

@ -40,5 +40,15 @@ namespace MediaBrowser.Model.Logging
/// Flushes this instance. /// Flushes this instance.
/// </summary> /// </summary>
void Flush(); void Flush();
/// <summary>
/// Adds the console output.
/// </summary>
void AddConsoleOutput();
/// <summary>
/// Removes the console output.
/// </summary>
void RemoveConsoleOutput();
} }
} }

View File

@ -1,10 +1,11 @@
<Properties> <Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release Mono" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Release Mono" />
<MonoDevelop.Ide.Workbench ActiveDocument="MediaBrowser.Server.Mono\Native\NativeApp.cs"> <MonoDevelop.Ide.Workbench ActiveDocument="MediaBrowser.Server.Mono\Program.cs">
<Files> <Files>
<File FileName="MediaBrowser.Server.Mono\app.config" Line="1" Column="1" /> <File FileName="MediaBrowser.Server.Mono\app.config" Line="1" Column="1" />
<File FileName="MediaBrowser.ServerApplication\ApplicationHost.cs" Line="1" Column="1" /> <File FileName="MediaBrowser.ServerApplication\ApplicationHost.cs" Line="1" Column="1" />
<File FileName="MediaBrowser.Server.Mono\Native\NativeApp.cs" Line="12" Column="23" /> <File FileName="MediaBrowser.Server.Mono\Native\NativeApp.cs" Line="12" Column="23" />
<File FileName="MediaBrowser.Server.Mono\Program.cs" Line="36" Column="34" />
</Files> </Files>
</MonoDevelop.Ide.Workbench> </MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>

View File

@ -64,7 +64,8 @@ namespace MediaBrowser.Providers.ImagesByName
.Replace(".", string.Empty) .Replace(".", string.Empty)
.Replace("&", string.Empty) .Replace("&", string.Empty)
.Replace("!", string.Empty) .Replace("!", string.Empty)
.Replace(",", string.Empty); .Replace(",", string.Empty)
.Replace("/", string.Empty);
} }
public static IEnumerable<string> GetAvailableImages(string file) public static IEnumerable<string> GetAvailableImages(string file)

View File

@ -32,7 +32,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
private string DefaultRedirectPath { get; set; } private string DefaultRedirectPath { get; set; }
private readonly ILogger _logger; private readonly ILogger _logger;
public string UrlPrefix { get; private set; } public IEnumerable<string> UrlPrefixes { get; private set; }
private readonly List<IRestfulService> _restServices = new List<IRestfulService>(); private readonly List<IRestfulService> _restServices = new List<IRestfulService>();
@ -66,7 +66,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
_containerAdapter = new ContainerAdapter(applicationHost); _containerAdapter = new ContainerAdapter(applicationHost);
for (var i = 0; i < 2; i++) for (var i = 0; i < 1; i++)
{ {
_autoResetEvents.Add(new AutoResetEvent(false)); _autoResetEvents.Add(new AutoResetEvent(false));
} }
@ -145,20 +145,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer
public override ServiceStackHost Start(string listeningAtUrlBase) public override ServiceStackHost Start(string listeningAtUrlBase)
{ {
StartListener(listeningAtUrlBase); StartListener();
return this; return this;
} }
/// <summary> /// <summary>
/// Starts the Web Service /// Starts the Web Service
/// </summary> /// </summary>
/// <param name="listeningAtUrlBase"> private void StartListener()
/// A Uri that acts as the base that the server is listening on.
/// Format should be: http://127.0.0.1:8080/ or http://127.0.0.1:8080/somevirtual/
/// Note: the trailing slash is required! For more info see the
/// HttpListener.Prefixes property on MSDN.
/// </param>
protected void StartListener(string listeningAtUrlBase)
{ {
// *** Already running - just leave it in place // *** Already running - just leave it in place
if (IsStarted) if (IsStarted)
@ -167,14 +161,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
if (Listener == null) if (Listener == null)
Listener = new HttpListener(); Listener = new HttpListener();
HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(listeningAtUrlBase); HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First());
UrlPrefix = listeningAtUrlBase; foreach (var prefix in UrlPrefixes)
{
Listener.Prefixes.Add(listeningAtUrlBase); _logger.Info("Adding HttpListener prefix " + prefix);
Listener.Prefixes.Add(prefix);
_logger.Info("Adding HttpListener Prefixes"); }
Listener.Prefixes.Add(listeningAtUrlBase);
IsStarted = true; IsStarted = true;
_logger.Info("Starting HttpListner"); _logger.Info("Starting HttpListner");
@ -419,7 +412,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{ {
if (Listener != null) if (Listener != null)
{ {
Listener.Prefixes.Remove(UrlPrefix); foreach (var prefix in UrlPrefixes)
{
Listener.Prefixes.Remove(prefix);
}
Listener.Close(); Listener.Close();
} }
@ -516,9 +512,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
public void StartServer(string urlPrefix) public void StartServer(IEnumerable<string> urlPrefixes)
{ {
Start(urlPrefix); UrlPrefixes = urlPrefixes.ToList();
Start(UrlPrefixes.First());
} }
public bool SupportsWebSockets public bool SupportsWebSockets

View File

@ -124,9 +124,9 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// <summary> /// <summary>
/// Starts this instance. /// Starts this instance.
/// </summary> /// </summary>
public void Start(string urlPrefix, bool enableHttpLogging) public void Start(IEnumerable<string> urlPrefixes, bool enableHttpLogging)
{ {
ReloadHttpServer(urlPrefix, enableHttpLogging); ReloadHttpServer(urlPrefixes, enableHttpLogging);
} }
public void StartWebSocketServer() public void StartWebSocketServer()
@ -153,14 +153,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// <summary> /// <summary>
/// Restarts the Http Server, or starts it if not currently running /// Restarts the Http Server, or starts it if not currently running
/// </summary> /// </summary>
private void ReloadHttpServer(string urlPrefix, bool enableHttpLogging) private void ReloadHttpServer(IEnumerable<string> urlPrefixes, bool enableHttpLogging)
{ {
// Only reload if the port has changed, so that we don't disconnect any active users
if (HttpServer != null && HttpServer.UrlPrefix.Equals(urlPrefix, StringComparison.OrdinalIgnoreCase))
{
return;
}
DisposeHttpServer(); DisposeHttpServer();
_logger.Info("Loading Http Server"); _logger.Info("Loading Http Server");
@ -169,7 +163,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager
{ {
HttpServer = _applicationHost.Resolve<IHttpServer>(); HttpServer = _applicationHost.Resolve<IHttpServer>();
HttpServer.EnableHttpRequestLogging = enableHttpLogging; HttpServer.EnableHttpRequestLogging = enableHttpLogging;
HttpServer.StartServer(urlPrefix); HttpServer.StartServer(urlPrefixes);
} }
catch (SocketException ex) catch (SocketException ex)
{ {

View File

@ -33,6 +33,7 @@ namespace MediaBrowser.Server.Mono
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server"); var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
logManager.ReloadLogger(LogSeverity.Info); logManager.ReloadLogger(LogSeverity.Info);
logManager.AddConsoleOutput();
var logger = _logger = logManager.GetLogger("Main"); var logger = _logger = logManager.GetLogger("Main");

View File

@ -94,11 +94,16 @@ namespace MediaBrowser.ServerApplication
/// Gets the HTTP server URL prefix. /// Gets the HTTP server URL prefix.
/// </summary> /// </summary>
/// <value>The HTTP server URL prefix.</value> /// <value>The HTTP server URL prefix.</value>
public string HttpServerUrlPrefix private IEnumerable<string> HttpServerUrlPrefixes
{ {
get get
{ {
return "http://+:" + ServerConfigurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/"; var list = new List<string>
{
"http://+:" + ServerConfigurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/"
};
return list;
} }
} }
@ -212,6 +217,8 @@ namespace MediaBrowser.ServerApplication
Logger.ErrorException("Error in {0}", ex, entryPoint.GetType().Name); Logger.ErrorException("Error in {0}", ex, entryPoint.GetType().Name);
} }
}); });
LogManager.RemoveConsoleOutput();
} }
/// <summary> /// <summary>
@ -462,7 +469,7 @@ namespace MediaBrowser.ServerApplication
{ {
try try
{ {
ServerManager.Start(HttpServerUrlPrefix, ServerConfigurationManager.Configuration.EnableHttpLevelLogging); ServerManager.Start(HttpServerUrlPrefixes, ServerConfigurationManager.Configuration.EnableHttpLevelLogging);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -494,7 +501,7 @@ namespace MediaBrowser.ServerApplication
HttpServer.EnableHttpRequestLogging = ServerConfigurationManager.Configuration.EnableHttpLevelLogging; HttpServer.EnableHttpRequestLogging = ServerConfigurationManager.Configuration.EnableHttpLevelLogging;
if (!string.Equals(HttpServer.UrlPrefix, HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) if (!HttpServer.UrlPrefixes.SequenceEqual(HttpServerUrlPrefixes, StringComparer.OrdinalIgnoreCase))
{ {
NotifyPendingRestart(); NotifyPendingRestart();
} }
@ -695,8 +702,10 @@ namespace MediaBrowser.ServerApplication
try try
{ {
ServerAuthorization.AuthorizeServer(ServerConfigurationManager.Configuration.HttpServerPortNumber, ServerAuthorization.AuthorizeServer(
HttpServerUrlPrefix, ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber, ServerConfigurationManager.Configuration.HttpServerPortNumber,
HttpServerUrlPrefixes.First(),
ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber,
UdpServerEntryPoint.PortNumber, UdpServerEntryPoint.PortNumber,
ConfigurationManager.CommonApplicationPaths.TempDirectory); ConfigurationManager.CommonApplicationPaths.TempDirectory);
} }

View File

@ -42,6 +42,7 @@ namespace MediaBrowser.ServerApplication
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server"); var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
logManager.ReloadLogger(LogSeverity.Debug); logManager.ReloadLogger(LogSeverity.Debug);
logManager.AddConsoleOutput();
var logger = _logger = logManager.GetLogger("Main"); var logger = _logger = logManager.GetLogger("Main");