如何让异常处理应用程序块(EHAB)从日志中的Exception.Data属性中写入值?
try
{
// ...
}
catch (Exception ex)
{
ex.Data.Add("Hello", "World");
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
正确记录异常,但我无法在EHAB创建的日志条目中的任何位置找到添加的数据.
据我所知,建议的做法是将其他相关信息添加到异常本身,如上例所示.这就是为什么我有点惊讶EHAB默认不包括这个.
我可以通过使用EntLib文本格式化模板编辑器(下面的屏幕截图)修改模板来解决这个问题吗?我找不到所提供的各种"令牌"的任何信息,但我认为答案隐藏在某处.
文本格式化模板编辑器http://img195.imageshack.us/img195/6614/capturegmg.png
或者我真的需要实现自己的自定义文本格式化程序来实现这一目标吗?
编辑/ UPDATE:
我在我的Global.asax.cs中这样做,以避免在我的代码中的任何地方添加HandleException方法调用:
using EntLib = Microsoft.Practices.EnterpriseLibrary;
using System;
namespace MyApp
{
public class Global : System.Web.HttpApplication
{
protected void Application_Error(object sender, EventArgs e)
{
// I have an "All Exceptions" policy in place...
EntLib.ExceptionHandling.ExceptionPolicy.HandleException(Server.GetLastError(), "All Exceptions");
// I believe it's the GetLastError that's somehow returning a "lessor" exception
}
}
}
Run Code Online (Sandbox Code Playgroud)
事实证明这与此不一样(工作正常,基本上解决了我的问题):
try
{
// ...
} …Run Code Online (Sandbox Code Playgroud) Validation.Validate()验证应用程序块的方法是否可以将IParameterCollectionUnity提供的每个参数视为我的自定义UserModel?
我试图使用Unity和验证应用程序块来验证方法参数.
我希望能够[RequiresValidation()]用适当的验证属性表示方法和该方法的参数.
所以,像这样:
[RequiresValidation()]
public void SaveUser(UserModel user)
{
// ...
}
public class UserModel
{
[StringLengthValidator(3, 255)]
[RegexValidator(@"^[a-zA-Z0-9]${3,255}")]
public string Name { get; set; }
[StringLengthValidator(0, 255)]
[RegexValidator(@"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b")]
public string EMail { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个自定义HandlerAttribute,它将启动对验证处理程序的调用,如图所示.
public class RequiresValidationAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new ValidationCallHandler();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,验证处理程序将尝试验证方法的每个参数:
public class ValidationCallHandler : ICallHandler
{
public int Order { get; set; }
public …Run Code Online (Sandbox Code Playgroud) c# validation enterprise-library unity-container validation-application-bl
我们正在尝试将一些动态可配置的日志记录实现到我们的Azure应用程序中,并且我们正在使用企业库来完成这项工作,但是为了实现这一点而需要的xml比ServiceConfiguration的简单"appSetting"样式设置更复杂. .cscfg文件似乎接受,因为它需要嵌套的xml节点,
例如
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
Run Code Online (Sandbox Code Playgroud)
解决了(请原谅这个窗口中的格式):
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
type="OurSolution.Common.AzureDiagnosticTraceListener, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="AzureDiagnosticTraceListener" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="AzureDiagnosticTraceListener" />
</listeners>
</add>
</categorySources>
</loggingConfiguration>
Run Code Online (Sandbox Code Playgroud)
我看不到愚弄ServiceDefinition或ServiceConfiguration文件来接受这个的方法,尽管我可以看到一种方法告诉企业库使用我可以在app.config中执行的ServiceConfiguration文件.
为什么我们试图解决这个问题是为了允许我们动态调整日志记录的设置,即从No logging更改为Verbose而不必进行重新部署,这在我们的实时应用程序中证明是耗时且不切实际的住,所以可能还有奇怪的虫子;-) …
嗨,您好,
我只是设置了Enterprise Library 5的日志记录应用程序块部分.我认为我已经完成了它,但它没有记录到事件日志,它在write方法上出错并给我以下异常.
The type LogWriter cannot be constructed.
You must configure the container to supply this value.
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我一张支票或告诉我我错过了什么..
首先,我使用winforms应用程序在Windows 7中运行.
这是我创建日志的方法,您可以看到write方法.
public class Logger : ILogger
{
public void SendTest(string test)
{
LogEntry log = new LogEntry();
log.EventId = 300;
log.Message = test;
log.Categories.Add("testing");
log.Severity = TraceEventType.Information;
log.Priority = 5;
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(log);
}
}
Run Code Online (Sandbox Code Playgroud)
我的所有配置都是使用entlib5编辑器而不是手工编写的.并确认这是entlib.config所在的路径
filePath="C:\myapp\entlib.config" />
Run Code Online (Sandbox Code Playgroud)
这是我的app.config,它指向我的entlib.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
<sources>
<add name="System …Run Code Online (Sandbox Code Playgroud) .net logging enterprise-library application-blocks logging-application-block
我们使用DiagnosticMonitorTraceListener作为通用跟踪侦听器(主要用于ASP.NET运行状况监视)以及用于异常处理的Enterprise Library 5侦听器.这在Azure上运行时效果很好,但重要的是我们能够以最小的更改在Azure之外运行网站.
一种选择是动态注册,如下所示:
protected void Application_Start()
{
if (Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable)
{
System.Diagnostics.Trace.Listeners.Add(new Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener());
System.Diagnostics.Trace.AutoFlush = true;
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于ASP.NET Health Monitoring和System.Diagnosics的一般用途,但不适用于我们具有以下硬编码配置的Enterprise Library:
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
<add name="Azure Diagnostics Trace Listener" />
</listeners>
</add>
</categorySources>
Run Code Online (Sandbox Code Playgroud)
如果没有解决,调用ExceptionPolicy.HandleException将生成:
未在托管服务或Development Fabric中运行.
要根据应用程序的运行位置有条件地删除它,我们可以使用EL5的流畅配置API,但必须重写我们的配置(它是全部或全部).
我们也可以使用web.config转换,除了已经有3种不同的解决方案配置(例如,dev,staging,production)之外,我们必须引入第4个来区分dev-standalone和dev-azure.
最后一个选择是创建一个自定义侦听器,它将所有消息路由到****(如果在Azure上运行)或什么都不做.
还有其他建议吗?
仅供参考,ASP.NET运行状况监视配置如下:
<healthMonitoring enabled="true">
<providers>
<add name="TraceWebProvider" type="System.Web.Management.TraceWebEventProvider" />
</providers>
<rules>
<add name="Application Events"
eventName="Application Lifetime Events"
provider="TraceWebProvider"
profile="Default"
minInstances="1"
maxLimit="Infinite"
minInterval="00:01:00" />
</rules>
</healthMonitoring>
Run Code Online (Sandbox Code Playgroud) 是否有任何.NET日志框架能够在当前问题出现问题时切换appender.其实我想要的是:
如果我正在使用数据库appender并且当数据库出现问题时(例如服务器停机,失去电源......)我想切换到第二个appender(例如,哪个登录到文件).
此功能是否具有以下功能之一:log4net,NLog,Enterprise Library?我在寻找这个,但没有运气.
我目前在我的几个应用程序中使用Enterprise Library的异常日志记录功能.
前段时间,我将Enterprise Library安装到我的机器上,以便从Visual Studio工具栏启动配置实用程序.在我的最新项目之前,一切都很棒.
几个星期前,我使用NuGet将我需要的企业库库下载到我的项目中.它提取的库是5.0.505.0版.我在我的机器上安装的企业库是5.0.414.0.我最初配置了项目中的所有内容,配置文件中的所有引用都设置为.414.当然,它不会运行,所以我将它们全部更改为.505,一切都运行了.现在问题是我需要进行一些更改,并且由于版本冲突,编辑器将无法运行.
所以,我检查了MSDN,我尝试下载此更新,但更新将无法运行,说明安装了较旧版本的Enterprise Library.
我考虑过卸载然后重新安装最新的企业库,这可能会有用.但是,我想检查是否有人知道更集成的升级方法?
有没有办法在不卸载和重新安装的情况下升级Enterprise Library?
自2011年2月以来,Unity是否改进了这个委托故事以实现与StructureMap的平等?
有谁知道什么是相当于SqlDbType.Bit的DbType?
我想转换
param[0] = new SqlParameter("@Status", SqlDbType.Bit);
param[0].Value = Status;
Run Code Online (Sandbox Code Playgroud)
至
db.AddInParameter(dbCommand, "@Status", <DbType dbType>, Status);
Run Code Online (Sandbox Code Playgroud)
但我不知道使用哪个DbType来表示单个Bit.有任何想法吗?
我们将云瞬态故障处理块应用于云服务的Web角色,我们甚至没有编写一个单行代码.当我们尝试在本地调试我们的云服务时,会在网页上显示异常,如下所示:
完全合格后路径太长.确保完整路径少于260个字符,目录名少于248个字符.
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.IO.FileLoadException:完全限定后路径太长.确保完整路径少于260个字符,目录名少于248个字符.
程序集加载跟踪:以下信息有助于确定无法加载程序集"Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ServiceBus"的原因.
=== Pre-bind state information ===
LOG: DisplayName = Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ServiceBus
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ServiceBus | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, …Run Code Online (Sandbox Code Playgroud) asp.net enterprise-library azure azure-web-roles azure-cloud-services