public void MediumTrustWithExternalClass() { var fileWritePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); try { int times = 25; { // ClassUnderTest must extend MarshalByRefObject AppDomain partialTrusted; var classUnderTest = MediumTrustContext.Create <ClassUnderTest>(fileWritePath, out partialTrusted); #if NET4_0 || NET4_5 using (NLog.NestedDiagnosticsLogicalContext.Push("PartialTrust")) #endif { classUnderTest.PartialTrustSuccess(times, fileWritePath); } AppDomain.Unload(partialTrusted); } // this also checks that thread-volatile layouts // such as ${threadid} are properly cached and not recalculated // in logging threads. var threadID = Thread.CurrentThread.ManagedThreadId.ToString(); Assert.False(File.Exists(Path.Combine(fileWritePath, "Trace.txt"))); AssertFileContents(Path.Combine(fileWritePath, "Debug.txt"), StringRepeat(times, "aaa " + threadID + "\n"), Encoding.UTF8); AssertFileContents(Path.Combine(fileWritePath, "Info.txt"), StringRepeat(times, "bbb " + threadID + "\n"), Encoding.UTF8); AssertFileContents(Path.Combine(fileWritePath, "Warn.txt"), StringRepeat(times, "ccc " + threadID + "\n"), Encoding.UTF8); AssertFileContents(Path.Combine(fileWritePath, "Error.txt"), StringRepeat(times, "ddd " + threadID + "\n"), Encoding.UTF8); AssertFileContents(Path.Combine(fileWritePath, "Fatal.txt"), StringRepeat(times, "eee " + threadID + "\n"), Encoding.UTF8); } finally { if (Directory.Exists(fileWritePath)) { Directory.Delete(fileWritePath, true); } // Clean up configuration change, breaks onetimeonlyexceptioninhandlertest LogManager.ThrowExceptions = true; } }
private static void RunAppDomainTestMethod(string fileWritePath, int times, bool autoShutdown) { // ClassUnderTest must extend MarshalByRefObject AppDomain partialTrusted = MediumTrustContext.CreatePartialTrustDomain(fileWritePath); var classUnderTest = (ClassUnderTest)partialTrusted.CreateInstanceAndUnwrap(typeof(ClassUnderTest).Assembly.FullName, typeof(ClassUnderTest).FullName); using (ScopeContext.PushProperty("Winner", new { Hero = "Zero" })) using (ScopeContext.PushNestedState(new { Hello = "World" })) { partialTrusted.DoCallBack(HelloWorld); classUnderTest.PartialTrustSuccess(times, fileWritePath, autoShutdown); } AppDomain.Unload(partialTrusted); }
private static void RunAppDomainTestMethod(string fileWritePath, int times, bool autoShutdown) { // ClassUnderTest must extend MarshalByRefObject AppDomain partialTrusted; var classUnderTest = MediumTrustContext.Create <ClassUnderTest>(fileWritePath, out partialTrusted); #if NET4_0 || NET4_5 MappedDiagnosticsLogicalContext.Set("Winner", new { Hero = "Zero" }); using (NestedDiagnosticsLogicalContext.Push(new { Hello = "World" })) #endif { classUnderTest.PartialTrustSuccess(times, fileWritePath, autoShutdown); } AppDomain.Unload(partialTrusted); }