I am tasked with writing ac# forms app to:
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)
Files being copied:
In our Global.asax, we are capturing errors like this:
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:
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:
<?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. I have tried several places to trip an error but naming a var wrong, missing a conn.open, etc, to no avail...
I have also tried bypassing the check for ' ShouldLogError()
' to ensure it fires but no go.
What other file or tweak is needed to get NLog working in this fashion?
There is a mistake in your config, there should be only one . Also the <targets>
(with multiple <target>
s in your case)<nlog>
was not closed - but that was probably a copy-paste error.
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. 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 .
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}. Removing those fixed it:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.