[英]NLog: custom installer program
I am tasked with writing ac# forms app to: 我的任务是编写ac#表单应用程序以:
This is meant for our Implementation Engineers to use as a deploy tool. 这是供我们的实施工程师用作部署工具。
(NOTE: THIS app is C#, but the target websites will always be VB as is our ecosystem) (注意:此应用是C#,但目标网站和我们的生态系统一样,始终是VB)
Files being copied: 正在复制的文件:
In our Global.asax, we are capturing errors like this: 在我们的Global.asax中,我们正在捕获如下错误:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
Dim lastException As Exception = Server.GetLastError()
HandleError(lastException)
End Sub
Public Shared Sub HandleError(ex As Exception)
'Honor the config setting for whether we should do the logging.
Dim hostId As String = ""
If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Request IsNot Nothing Then
hostId = " (" + HttpContext.Current.Request.Url.Host + ")"
End If
If ex Is Nothing Then
'OrElse request.Url.Host.ToLower = "localhost" Then
Return
End If
If ShouldLogError(ex) Then
Dim LogManager As New LogManager()
LogManager.Logger.LogError("Global Exception",ex)
End If
End Sub
Protected Shared Function ShouldLogError(ex As Exception) As Boolean
If TypeOf ex Is System.Web.HttpRequestValidationException Then
Return False
End If
If TypeOf ex Is HttpException Then
Dim exHttp As HttpException = DirectCast(ex, HttpException)
Select Case exHttp.GetHttpCode()
Case 404
'File Not Found
Return False
End Select
End If
If TypeOf ex Is System.IO.FileNotFoundException Then
Return False
End If
If TypeOf ex.InnerException Is System.IO.FileNotFoundException Then
Return False
End If
If TypeOf ex Is System.Reflection.TargetInvocationException OrElse TypeOf ex Is System.FormatException OrElse (TypeOf ex Is HttpException AndAlso ex.Message.Contains("Invalid viewstate.")) OrElse TypeOf ex Is System.Security.Cryptography.CryptographicException Then
If HttpContext.Current.Request.Url.AbsolutePath.EndsWith("ScriptResource.axd", StringComparison.OrdinalIgnoreCase) OrElse HttpContext.Current.Request.Url.AbsolutePath.EndsWith("WebResource.axd", StringComparison.OrdinalIgnoreCase) Then
Return False
End If
End If
Return True
End Function
in LogManager.vb: 在LogManager.vb中:
Public Class LogManager
Private _Logger As ILogger
Public Sub New()
End Sub
Public ReadOnly Property Logger As ILogger
Get
If _Logger Is Nothing Then
_Logger = New LoggerFactory().CreateLoggerNlog("Website")
End If
Return _Logger
End Get
End Property
End Class
and the NLog.config: 和NLog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="OURDOMAINlibrary"/>
<!--<add assembly="NLog.Extended"/>-->
</extensions>
<targets async="false">
<target xsi:type="Console" name="debugConsole" layout="${VnEventId:brackets=true} ${uppercase:${level}} ${LoggerFriendly} ${message} "/>
</targets>
<targets async="true">
<target xsi:type="File" name="debugFile" createDirs="true" fileName="c:/Vendornet/Log/${iis-site-name}}/${iis-site-name}_${shortdate}.log" layout="${longdate} ${VnEventId:brackets=true} ${uppercase:${level}} ${LoggerFriendly} ${message}"/>
<!-- Alter the smtpServer and to attributes only. For information regarding the ${FatalEmail} tag, see
the documentation provided with this project
-->
<target name="mail" xsi:type="Mail" html="true" replaceNewlineWithBrTagInHtml="true" smtpServer="OURDOMAIN" smtpAuthentication="None" to="${FatalEmail:DL-OURDOMAIN-ImpDev@OURDOMAIN.com;me@OURDOMAIN.com}" from="${machinename}@OURDOMAIN.com" subject="${iis-site-name} - ${processname} has FAILED on ${machinename}." body="${message}"/>
<!-- The Webservice Target Below should not be modified except for the url attribute. To disable this target
set the minlevel in the Rules section to Off. Otherwise set it to Trace or higher
-->
<target type="WebService" name="vnLogApi" url="http://OURDOMAIN/api/log" protocol="HttpPost" encoding="UTF-8">
<parameter name="time_stamp" type="System.String" layout="${date}"/>
<parameter name="level" type="System.String" layout="${level}"/>
<parameter name="logger" type="System.String" layout="${logger}"/>
<parameter name="userName" type="System.String" layout="${identity}"/>
<parameter name="url" type="System.String" layout="${aspnet-request:serverVariable=Url}"/>
<parameter name="machineName" type="System.String" layout="${machinename}"/>
<parameter name="sessionId" type="System.String" layout="${aspnet-sessionid}"/>
<parameter name="threadId" type="System.String" layout="${threadid}"/>
<parameter name="referrer" type="System.String" layout="${aspnet-request:serverVariable=HTTP_REFERER}"/>
<parameter name="userAgent" type="System.String" layout="${aspnet-request:serverVariable=HTTP_USER_AGENT}"/>
<parameter name="code" type="System.String" layout="${CUSTOMEventId}"/>
<parameter name="message" type="System.String" layout="${message}"/>
<parameter name="version" type="System.String" layout=""/>
<parameter name="exception" type="System.String" layout="${exception}"/>
<parameter name="stackTrace" type="System.String" layout="${stacktrace}"/>
<parameter name="clientName" type="System.String" layout="${iis-site-name}"/>
<parameter name="fileName" type="System.String" layout="${iis-site-name} ${date}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="debugConsole"/>
<logger name="*" minlevel="Trace" writeTo="debugFile"/>
<!-- SET TO Trace or Higher Once LogAPI is available for this server -->
<logger name="*" minlevel="Trace" writeTo="vnLogApi"/>
<!-- THIS LINE SHOULD NOT BE MODIFIED -->
<logger name="*" minlevel="Fatal" writeTo="mail"/>
</rules>
So, even with the most stripped down NLog.config it is not outputting anything... tried a simple log file, webservice mail... nothing. 因此,即使对NLog.config进行了最简化的配置,它也不输出任何内容……尝试了一个简单的日志文件,Web服务邮件……什么也没有。 I have tried several places to trip an error but naming a var wrong, missing a conn.open, etc, to no avail... 我已经尝试了几个地方来解决一个错误,但命名为var错误,缺少conn.open等,无济于事...
I have also tried bypassing the check for ' ShouldLogError()
' to ensure it fires but no go. 我还尝试绕过对“ ShouldLogError()
”的检查,以确保它触发但ShouldLogError()
。
What other file or tweak is needed to get NLog working in this fashion? 要使NLog以这种方式工作,还需要其他什么文件或调整?
There is a mistake in your config, there should be only one . 您的配置中有一个错误, <targets>
(with multiple <target>
s in your case)应该只有一个 。 Also the <targets>
(在您的情况下有多个<target>
)<nlog>
was not closed - but that was probably a copy-paste error. 此外, <nlog>
也没有关闭-但这可能是复制粘贴错误。
It should be: 它应该是:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="OURDOMAINlibrary"/>
<!--<add assembly="NLog.Extended"/>-->
</extensions>
<targets async="true">
<target xsi:type="Console" name="debugConsole" layout="${VnEventId:brackets=true} ${uppercase:${level}} ${LoggerFriendly} ${message} "/>
<target xsi:type="File" name="debugFile" createDirs="true" fileName="c:/Vendornet/Log/${iis-site-name}}/${iis-site-name}_${shortdate}.log" layout="${longdate} ${VnEventId:brackets=true} ${uppercase:${level}} ${LoggerFriendly} ${message}"/>
<!-- Alter the smtpServer and to attributes only. For information regarding the ${FatalEmail} tag, see
the documentation provided with this project
-->
<target name="mail" xsi:type="Mail" html="true" replaceNewlineWithBrTagInHtml="true" smtpServer="OURDOMAIN" smtpAuthentication="None" to="${FatalEmail:DL-OURDOMAIN-ImpDev@OURDOMAIN.com;me@OURDOMAIN.com}" from="${machinename}@OURDOMAIN.com" subject="${iis-site-name} - ${processname} has FAILED on ${machinename}." body="${message}"/>
<!-- The Webservice Target Below should not be modified except for the url attribute. To disable this target
set the minlevel in the Rules section to Off. Otherwise set it to Trace or higher
-->
<target type="WebService" name="vnLogApi" url="http://OURDOMAIN/api/log" protocol="HttpPost" encoding="UTF-8">
<parameter name="time_stamp" type="System.String" layout="${date}"/>
<parameter name="level" type="System.String" layout="${level}"/>
<parameter name="logger" type="System.String" layout="${logger}"/>
<parameter name="userName" type="System.String" layout="${identity}"/>
<parameter name="url" type="System.String" layout="${aspnet-request:serverVariable=Url}"/>
<parameter name="machineName" type="System.String" layout="${machinename}"/>
<parameter name="sessionId" type="System.String" layout="${aspnet-sessionid}"/>
<parameter name="threadId" type="System.String" layout="${threadid}"/>
<parameter name="referrer" type="System.String" layout="${aspnet-request:serverVariable=HTTP_REFERER}"/>
<parameter name="userAgent" type="System.String" layout="${aspnet-request:serverVariable=HTTP_USER_AGENT}"/>
<parameter name="code" type="System.String" layout="${CUSTOMEventId}"/>
<parameter name="message" type="System.String" layout="${message}"/>
<parameter name="version" type="System.String" layout=""/>
<parameter name="exception" type="System.String" layout="${exception}"/>
<parameter name="stackTrace" type="System.String" layout="${stacktrace}"/>
<parameter name="clientName" type="System.String" layout="${iis-site-name}"/>
<parameter name="fileName" type="System.String" layout="${iis-site-name} ${date}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="debugConsole"/>
<logger name="*" minlevel="Trace" writeTo="debugFile"/>
<!-- SET TO Trace or Higher Once LogAPI is available for this server -->
<logger name="*" minlevel="Trace" writeTo="vnLogApi"/>
<!-- THIS LINE SHOULD NOT BE MODIFIED -->
<logger name="*" minlevel="Fatal" writeTo="mail"/>
</rules>
</nlog>
Please keep in mind you can only with the async attribute all targets. 请记住,您只能使用async属性来实现所有目标。 If you need just one target async, you should use the async wrapper: 如果只需要一个目标异步,则应使用异步包装器:
<targets>
<target name="target2" xsi:type="AsyncWrapper">
<target name ="target1" xsi:type="File"
fileName="c:/temp/test.log" layout="${message}"
keepFileOpen="true" />
</target>
<rules>
<logger name="*" minlevel="Info" writeTo="target2"/>
</rules>
</targets>
See the AsyncWrapper target docs . 请参阅AsyncWrapper目标文档 。
If you still experience problems, enable and check the internal log 如果仍然遇到问题,请启用并检查内部日志
Apparently the person before me wrote some of NLog into one of our custom dlls but not NLog.web, which is needed for some of the layouts like {iis-site-name}. 显然,在我之前的人将一些NLog写入了我们的自定义dll之一,但没有将NLog.web写入某些布局(例如{iis-site-name})所需要的NLog.web。 Removing those fixed it: 删除那些修复它:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.