2012-02-07 17 views
11

Khi tôi gỡ xuống cơ sở dữ liệu mà trở lại làm tắc nghẽn, không có gì được đăng nhập và có vẻ như NLog nuốt vấn đề. Có cách nào để cấu hình nó để nâng cao và ngoại lệ hoặc ít nhất là để đăng nhập vào một tập tin văn bản mà đăng nhập thất bại?Làm thế nào để buộc nlog ném một ngoại lệ khi đăng nhập vào cơ sở dữ liệu không thành công?

Đây là những gì cấu hình của tôi trông giống như:

<?xml version="1.0" ?> 
<nlog autoReload="true" throwExceptions="true" internalLogFile="${basedir}/App_Data/nlog.txt" internalLogLevel="Debug" 
internalLogToConsole="true"> 

<targets> 
<!--Useful for debugging--> 
<target name="consolelog" type="ColoredConsole" 
layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" /> 



<target name="databaselog" type="Database"> 

<dbProvider>System.Data.SqlClient</dbProvider> 

<!-- database connection parameters --> 
<!-- alternatively you could provide a single 'connectionstring' parameter --> 
<connectionString>Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI</connectionString> 

<commandText> 
insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(@time_stamp,@level,@host,@type,@source,@logger,@message,@stacktrace,@allxml); 
</commandText> 

<parameter name="@time_stamp" layout="${utc_date}" /> 
<parameter name="@level" layout="${level}" /> 
<parameter name="@host" layout="${machinename}" /> 
<parameter name="@type" layout="${exception:format=type}" /> 
<parameter name="@source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" /> 
<parameter name="@logger" layout="${logger}" /> 
<parameter name="@message" layout="${message}" /> 
<parameter name="@stacktrace" layout="${exception:stacktrace}" /> 
<parameter name="@allxml" layout="${web_variables}" /> 

</target> 

</targets> 

<rules> 

<logger name="*" minlevel="Info" writeTo="databaselog" /> 
</rules> 

</nlog> 

Trả lời

14

Bạn có thể buộc nLog để ném ngoại lệ khi máy chủ sql không đạt được bằng cách làm theo thông tin

<nlog throwExceptions="true"> 
... your nlog config 
</nlog> 

More đây,

http://nlog-project.org/2010/09/05/new-exception-handling-rules-in-nlog-2-0.html

Đó là một tính năng mới trong v2.0 vì vậy bạn cần v2.0.

Nó sẽ không hoạt động trong các phiên bản trước đó.

Cũng kiểm sau thông tin cấu hình

https://github.com/NLog/NLog/wiki/Logging-Troubleshooting

cho phép nLog để đăng nhập đó là trường hợp ngoại lệ riêng vào một tập tin cụ thể.

2
  1. Liệu NLog.config có tài sản "Sao chép vào Output Directory" thiết lập như "Sao chép luôn"?
  2. Tôi nghĩ rằng bạn có sai tệp NLog.config: bạn sử dụng các phần tử thay vì các thuộc tính trong mục tiêu (documentation). Nên một cái gì đó như thế này:

<target 
    name="databaselog" 
    type="Database" 
    dbProvider="System.Data.SqlClient" 
    connectionString="Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI" 
    commandText="insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(@time_stamp,@level,@host,@type,@source,@logger,@message,@stacktrace,@allxml);"> 
    <parameter name="@time_stamp" layout="${utc_date}" /> 
    <parameter name="@level" layout="${level}" /> 
    <parameter name="@host" layout="${machinename}" /> 
    <parameter name="@type" layout="${exception:format=type}" /> 
    <parameter name="@source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" /> 
    <parameter name="@logger" layout="${logger}" /> 
    <parameter name="@message" layout="${message}" /> 
    <parameter name="@stacktrace" layout="${exception:stacktrace}" /> 
    <parameter name="@allxml" layout="${web_variables}" /> 
</target> 
+0

1) Nó không có, nhưng nó đã hoạt động ... cảm ơn mặc dù tôi đã đặt nó vào Sao chép Luôn ngay bây giờ. 2) Việc đăng nhập thực sự hoạt động tốt, nhưng vấn đề của tôi là nếu tôi tắt máy chủ SQL, NLog sẽ không ném bất kỳ ngoại lệ hoặc đăng nhập bất cứ nơi nào mà nó không thể đăng nhập vào cơ sở dữ liệu. Tôi muốn có thể nắm bắt và xử lý các ngoại lệ của NLog. – m0s

Các vấn đề liên quan