Skip to content

Commit

Permalink
Updates to better support Silverlight
Browse files Browse the repository at this point in the history
Added support for NLog.config inside xap file for Silverlight
Enabled include file support for Silverlight (inside XAP).
Updated MessageBoxTarget to always show message box on the UI thread.
Added ${sl-appinfo} layout renderer.
  • Loading branch information
jkowalski committed Jun 23, 2010
1 parent 17e016b commit 215106a
Show file tree
Hide file tree
Showing 41 changed files with 733 additions and 83 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,5 +1,6 @@
obj/
bin/
Bin/
msbuild.log
LastTestRunSummary.cmd
src/Docs/Working/
Expand All @@ -15,5 +16,4 @@ StyleCop.Cache
NLogMerged.api.xml
_UpgradeReport_Files/
UpgradeLog*.XML


*.xap
6 changes: 6 additions & 0 deletions src/NLog.sl4.sln
Expand Up @@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLog.UnitTests.sl4", "..\te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleExtensions.sl4", "..\tests\SampleExtensions\SampleExtensions.sl4.csproj", "{C480452F-7E14-443D-906D-7E021AB5707A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SilverlightApp.sl4", "..\tests\SilverlightApp\SilverlightApp.sl4.csproj", "{6BDB11AF-E5F0-43D2-B823-371E5B79087C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,6 +32,10 @@ Global
{C480452F-7E14-443D-906D-7E021AB5707A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C480452F-7E14-443D-906D-7E021AB5707A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C480452F-7E14-443D-906D-7E021AB5707A}.Release|Any CPU.Build.0 = Release|Any CPU
{6BDB11AF-E5F0-43D2-B823-371E5B79087C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BDB11AF-E5F0-43D2-B823-371E5B79087C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BDB11AF-E5F0-43D2-B823-371E5B79087C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BDB11AF-E5F0-43D2-B823-371E5B79087C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
20 changes: 19 additions & 1 deletion src/NLog/Config/XmlLoggingConfiguration.cs
Expand Up @@ -141,7 +141,11 @@ public XmlLoggingConfiguration(XmlReader reader, string fileName, bool ignoreErr
if (fileName != null)
{
InternalLogger.Info("Configuring from an XML element in {0}...", fileName);
#if SILVERLIGHT
string key = fileName;
#else
string key = Path.GetFullPath(fileName);
#endif
this.visitedFile[key] = true;

this.originalFileName = fileName;
Expand Down Expand Up @@ -214,7 +218,12 @@ public override LoggingConfiguration Reload()

private void ConfigureFromFile(string fileName)
{
#if SILVERLIGHT
// file names are relative to XAP
string key = fileName;
#else
string key = Path.GetFullPath(fileName);
#endif
if (this.visitedFile.ContainsKey(key))
{
return;
Expand Down Expand Up @@ -807,9 +816,18 @@ private void ParseIncludeElement(XmlReader reader, string baseDirectory)

try
{
newFileName = Path.Combine(baseDirectory, SimpleLayout.Evaluate(newFileName));
newFileName = SimpleLayout.Evaluate(newFileName);
if (baseDirectory != null)
{
newFileName = Path.Combine(baseDirectory, newFileName);
}

#if SILVERLIGHT
newFileName = newFileName.Replace("\\", "/");
if (Application.GetResourceStream(new Uri(newFileName, UriKind.Relative)) != null)
#else
if (File.Exists(newFileName))
#endif
{
InternalLogger.Debug("Including file '{0}'", newFileName);
this.ConfigureFromFile(newFileName);
Expand Down
105 changes: 105 additions & 0 deletions src/NLog/LayoutRenderers/SilverlightApplicationInfoLayoutRenderer.cs
@@ -0,0 +1,105 @@
//
// Copyright (c) 2004-2010 Jaroslaw Kowalski <jaak@jkowalski.net>
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

#if SILVERLIGHT || DOCUMENTATION

namespace NLog.LayoutRenderers
{
using System;
using System.ComponentModel;
using System.Text;
#if !DOCUMENTATION
using System.Windows;
using System.Windows.Browser;
using NLog.Config;

#endif

/// <summary>
/// Information about Silverlight application.
/// </summary>
[LayoutRenderer("sl-appinfo")]
public class SilverlightApplicationInfoLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Initializes a new instance of the <see cref="SilverlightApplicationInfoLayoutRenderer"/> class.
/// </summary>
public SilverlightApplicationInfoLayoutRenderer()
{
this.Option = SilverlightApplicationInfoOption.XapUri;
}

/// <summary>
/// Gets or sets specific information to display.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
[DefaultParameter]
[DefaultValue(SilverlightApplicationInfoOption.XapUri)]
public SilverlightApplicationInfoOption Option { get; set; }

/// <summary>
/// Renders the specified environmental information and appends it to the specified <see cref="StringBuilder"/>.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
#if !DOCUMENTATION
switch (this.Option)
{
case SilverlightApplicationInfoOption.XapUri:
builder.Append(Application.Current.Host.Source);
break;

#if !SILVERLIGHT2
case SilverlightApplicationInfoOption.IsOutOfBrowser:
builder.Append(Application.Current.IsRunningOutOfBrowser ? "1" : "0");
break;

case SilverlightApplicationInfoOption.InstallState:
builder.Append(Application.Current.InstallState);
break;
#if !SILVERLIGHT3
case SilverlightApplicationInfoOption.HasElevatedPermissions:
builder.Append(Application.Current.HasElevatedPermissions ? "1" : "0");
break;
#endif

#endif
}
#endif
}
}
}

#endif
66 changes: 66 additions & 0 deletions src/NLog/LayoutRenderers/SilverlightApplicationInfoOption.cs
@@ -0,0 +1,66 @@
//
// Copyright (c) 2004-2010 Jaroslaw Kowalski <jaak@jkowalski.net>
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

namespace NLog.LayoutRenderers
{
/// <summary>
/// Specifies application information to display in ${sl-appinfo} renderer.
/// </summary>
public enum SilverlightApplicationInfoOption
{
/// <summary>
/// URI of the current application XAP file.
/// </summary>
XapUri,

#if !SILVERLIGHT2
/// <summary>
/// Whether application is running out-of-browser.
/// </summary>
IsOutOfBrowser,

/// <summary>
/// Installed state of an application.
/// </summary>
InstallState,

#if !SILVERLIGHT3
/// <summary>
/// Whether application is running with elevated permissions.
/// </summary>
HasElevatedPermissions,
#endif

#endif
}
}
14 changes: 11 additions & 3 deletions src/NLog/LogFactory.cs
Expand Up @@ -42,6 +42,7 @@ namespace NLog
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Windows;
using NLog.Common;
using NLog.Config;
using NLog.Internal;
Expand Down Expand Up @@ -136,19 +137,26 @@ public LoggingConfiguration Configuration
}
#endif

#if !SILVERLIGHT
if (this.config == null)
{
foreach (string configFile in GetCandidateFileNames())
{
#if !SILVERLIGHT
if (File.Exists(configFile))
{
InternalLogger.Debug("Attempting to load config from {0}", configFile);
this.config = new XmlLoggingConfiguration(configFile);
}
#else
Uri configFileUri = new Uri(configFile, UriKind.Relative);
if (Application.GetResourceStream(configFileUri) != null)
{
InternalLogger.Debug("Attempting to load config from {0}", configFile);
this.config = new XmlLoggingConfiguration(configFile);
}
#endif
}
}
#endif

#if !NET_CF && !SILVERLIGHT
if (this.config != null)
Expand Down Expand Up @@ -575,7 +583,7 @@ private static IEnumerable<string> GetCandidateFileNames()
yield return Path.Combine(Path.GetDirectoryName(CompactFrameworkHelper.GetExeFileName()), "NLog.config");
yield return typeof(LogFactory).Assembly.GetName().CodeBase + ".nlog";
#elif SILVERLIGHT
yield break;
yield return "NLog.config";
#else
// NLog.config from application directory
yield return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NLog.config");
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.doc.csproj
Expand Up @@ -213,6 +213,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.mono2.csproj
Expand Up @@ -217,6 +217,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.netcf20.csproj
Expand Up @@ -225,6 +225,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.netcf35.csproj
Expand Up @@ -226,6 +226,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.netfx20.csproj
Expand Up @@ -215,6 +215,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.netfx35.csproj
Expand Up @@ -211,6 +211,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.netfx40.csproj
Expand Up @@ -213,6 +213,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.sl2.csproj
Expand Up @@ -219,6 +219,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.sl3.csproj
Expand Up @@ -219,6 +219,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.sl4.csproj
Expand Up @@ -220,6 +220,8 @@
<Compile Include="LayoutRenderers\QpcLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\RegistryLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\ShortDateLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\SilverlightApplicationInfoOption.cs" />
<Compile Include="LayoutRenderers\SpecialFolderLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\StackTraceFormat.cs" />
<Compile Include="LayoutRenderers\StackTraceLayoutRenderer.cs" />
Expand Down

0 comments on commit 215106a

Please sign in to comment.