// namespace DotNetOpenAuth.Loggers { using System; using System.Diagnostics; using System.Security; using System.Security.Permissions; internal class TraceLogger : ILog { private TraceSwitch traceSwitch; internal TraceLogger(string name) { traceSwitch = new TraceSwitch(name, name + " Trace Switch"); } #region ILog Properties /// /// See . /// public bool IsDebugEnabled { get { return this.traceSwitch.TraceVerbose; } } /// /// See . /// public bool IsInfoEnabled { get { return this.traceSwitch.TraceInfo; } } /// /// See . /// public bool IsWarnEnabled { get { return this.traceSwitch.TraceWarning; } } /// /// See . /// public bool IsErrorEnabled { get { return this.traceSwitch.TraceError; } } /// /// See . /// public bool IsFatalEnabled { get { return this.traceSwitch.TraceError; } } #endregion private static bool IsSufficientPermissionGranted { get { PermissionSet permissions = new PermissionSet(PermissionState.None); permissions.AddPermission(new KeyContainerPermission(PermissionState.Unrestricted)); permissions.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)); permissions.AddPermission(new RegistryPermission(PermissionState.Unrestricted)); permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.UnmanagedCode | SecurityPermissionFlag.ControlThread)); var file = new FileIOPermission(PermissionState.None); file.AllFiles = FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read; permissions.AddPermission(file); try { permissions.Demand(); return true; } catch (SecurityException) { return false; } } } #region ILog Methods /// /// See . /// public void Debug(object message) { if (this.IsDebugEnabled) { Trace.TraceInformation(message.ToString()); } } /// /// See . /// public void Debug(object message, Exception exception) { if (this.IsDebugEnabled) { Trace.TraceInformation(message + ": " + exception.ToString()); } } /// /// See . /// public void DebugFormat(string format, params object[] args) { if (this.IsDebugEnabled) { Trace.TraceInformation(format, args); } } /// /// See . /// public void DebugFormat(string format, object arg0) { if (this.IsDebugEnabled) { Trace.TraceInformation(format, arg0); } } /// /// See . /// public void DebugFormat(string format, object arg0, object arg1) { if (this.IsDebugEnabled) { Trace.TraceInformation(format, arg0, arg1); } } /// /// See . /// public void DebugFormat(string format, object arg0, object arg1, object arg2) { if (this.IsDebugEnabled) { Trace.TraceInformation(format, arg0, arg1, arg2); } } /// /// See . /// public void Info(object message) { if (this.IsInfoEnabled) { Trace.TraceInformation(message.ToString()); } } /// /// See . /// public void Info(object message, Exception exception) { if (this.IsInfoEnabled) { Trace.TraceInformation(message + ": " + exception.ToString()); } } /// /// See . /// public void InfoFormat(string format, params object[] args) { if (this.IsInfoEnabled) { Trace.TraceInformation(format, args); } } /// /// See . /// public void InfoFormat(string format, object arg0) { if (this.IsInfoEnabled) { Trace.TraceInformation(format, arg0); } } /// /// See . /// public void InfoFormat(string format, object arg0, object arg1) { if (this.IsInfoEnabled) { Trace.TraceInformation(format, arg0, arg1); } } /// /// See . /// public void InfoFormat(string format, object arg0, object arg1, object arg2) { if (this.IsInfoEnabled) { Trace.TraceInformation(format, arg0, arg1, arg2); } } /// /// See . /// public void Warn(object message) { if (this.IsWarnEnabled) { Trace.TraceWarning(message.ToString()); } } /// /// See . /// public void Warn(object message, Exception exception) { if (this.IsWarnEnabled) { Trace.TraceWarning(message + ": " + exception.ToString()); } } /// /// See . /// public void WarnFormat(string format, params object[] args) { if (this.IsWarnEnabled) { Trace.TraceWarning(format, args); } } /// /// See . /// public void WarnFormat(string format, object arg0) { if (this.IsWarnEnabled) { Trace.TraceWarning(format, arg0); } } /// /// See . /// public void WarnFormat(string format, object arg0, object arg1) { if (this.IsWarnEnabled) { Trace.TraceWarning(format, arg0, arg1); } } /// /// See . /// public void WarnFormat(string format, object arg0, object arg1, object arg2) { if (this.IsWarnEnabled) { Trace.TraceWarning(format, arg0, arg1, arg2); } } /// /// See . /// public void Error(object message) { if (this.IsErrorEnabled) { Trace.TraceError(message.ToString()); } } /// /// See . /// public void Error(object message, Exception exception) { if (this.IsErrorEnabled) { Trace.TraceError(message + ": " + exception.ToString()); } } /// /// See . /// public void ErrorFormat(string format, params object[] args) { if (this.IsErrorEnabled) { Trace.TraceError(format, args); } } /// /// See . /// public void ErrorFormat(string format, object arg0) { if (this.IsErrorEnabled) { Trace.TraceError(format, arg0); } } /// /// See . /// public void ErrorFormat(string format, object arg0, object arg1) { if (this.IsErrorEnabled) { Trace.TraceError(format, arg0, arg1); } } /// /// See . /// public void ErrorFormat(string format, object arg0, object arg1, object arg2) { if (this.IsErrorEnabled) { Trace.TraceError(format, arg0, arg1, arg2); } } /// /// See . /// public void Fatal(object message) { if (this.IsFatalEnabled) { Trace.TraceError(message.ToString()); } } /// /// See . /// public void Fatal(object message, Exception exception) { if (this.IsFatalEnabled) { Trace.TraceError(message + ": " + exception.ToString()); } } /// /// See . /// public void FatalFormat(string format, params object[] args) { if (this.IsFatalEnabled) { Trace.TraceError(format, args); } } /// /// See . /// public void FatalFormat(string format, object arg0) { if (this.IsFatalEnabled) { Trace.TraceError(format, arg0); } } /// /// See . /// public void FatalFormat(string format, object arg0, object arg1) { if (this.IsFatalEnabled) { Trace.TraceError(format, arg0, arg1); } } /// /// See . /// public void FatalFormat(string format, object arg0, object arg1, object arg2) { if (this.IsFatalEnabled) { Trace.TraceError(format, arg0, arg1, arg2); } } #endregion /// /// Returns a new logger that uses the class /// if sufficient CAS permissions are granted to use it, otherwise returns false. /// /// The created instance. internal static ILog Initialize(string name) { return IsSufficientPermissionGranted ? new TraceLogger(name) : null; } } }