I am experiencing following behavior:
In development (local) there is no problem logging exceptions, all kind works.
When I logged in remote server ( by remote desktop connection ) and open a chrome inside the server and create the exception, there is also no problem, all kind of logging works.
If I use my laptop and Chrome Browser to open the same website that is hosted on the server:
Controller/Action
{
// log record gets created for the following line
logger.Log(LogLevel.Fatal, "Sample informational message");
// log record not created. However this record get created in item 1 and 2 case above
int a = int.Parse("A");
}
Global.asax
protected void Application_Error(object sender, EventArgs e)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.Fatal(Server.GetLastError());
Server.ClearError();
}
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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off"
internalLogFile="D:\...\logs\nlog-internal.log">
<!--Define Various Log Targets like files, database or asp.net trace files-->
<targets>
<target name="console" xsi:type="ColoredConsole" layout="${message}" />
<!--Write logs to File where we want to write error logs-->
<target name="file"
archiveFileName="logs/archives/app-log.{#}.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7"
xsi:type="File"
fileName="D:\...\logs\ErrorLogFile.log"
layout="--------------------- ${level} (${longdate}) ${machinename} --------------------
${newline}
${newline}
Additional Info : ${message}${newline}
Exception Type : ${exception:format=Type}${newline}
Exception Message: ${exception:format=Message}${newline}
Stack Trace : ${exception:format=Stack Trace}${newline}
">
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
internalLogLevel="Debug" output:
2018-09-20 19:26:10.4825 Debug ScanAssembly('NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2018-09-20 19:26:10.5275 Debug Search for auto loading files, location: C:\inetpub\wwwroot\Test
2018-09-20 19:26:10.5275 Debug Start auto loading, location: C:\inetpub\wwwroot\Test
2018-09-20 19:26:10.5345 Debug Auto loading done
2018-09-20 19:26:10.5345 Info Message Template Auto Format enabled
2018-09-20 19:26:10.5345 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2018-09-20 19:26:10.5345 Debug Setting 'ColoredConsoleTarget.name' to 'console'
2018-09-20 19:26:10.5345 Debug Setting 'ColoredConsoleTarget.layout' to '${message}'
2018-09-20 19:26:10.5345 Info Adding target ColoredConsole Target[console]
2018-09-20 19:26:10.5345 Debug Registered target console: NLog.Targets.ColoredConsoleTarget
2018-09-20 19:26:10.5525 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.name' to 'file'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.archiveFileName' to 'logs/archives/app-log.{#}.txt'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.archiveEvery' to 'Day'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.archiveNumbering' to 'Rolling'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.maxArchiveFiles' to '7'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.fileName' to 'C:\temp\logs\ErrorLogFile.log'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.layout' to '--------------------- ${level} (${longdate}) ${machinename} -------------------- ${newline} ${newline} Additional Info : ${message}${newline} Exception Type : ${exception:format=Type}${newline} Exception Message: ${exception:format=Message}${newline} Stack Trace : ${exception:format=Stack Trace}${newline} '
2018-09-20 19:26:10.5795 Debug Setting 'ExceptionLayoutRenderer.format' to 'Type'
2018-09-20 19:26:10.5815 Debug Setting 'ExceptionLayoutRenderer.format' to 'Message'
2018-09-20 19:26:10.5815 Debug Setting 'ExceptionLayoutRenderer.format' to 'Stack Trace'
2018-09-20 19:26:10.5815 Info Adding target File Target[file]
2018-09-20 19:26:10.5815 Debug Registered target file: NLog.Targets.FileTarget
2018-09-20 19:26:10.5815 Info Configured from an XML element in C:\inetpub\wwwroot\Test\NLog.config...
2018-09-20 19:26:10.5815 Debug Unused target checking is started... Rule Count: 1, Target Count: 2
2018-09-20 19:26:10.5815 Warn Unused target detected. Add a rule for this target to the configuration. TargetName: console
2018-09-20 19:26:10.5815 Debug Unused target checking is completed. Total Rule Count: 1, Total Target Count: 2, Unused Target Count: 1
2018-09-20 19:26:10.5815 Debug --- NLog configuration dump ---
2018-09-20 19:26:10.5815 Debug Targets:
2018-09-20 19:26:10.5815 Debug ColoredConsole Target[console]
2018-09-20 19:26:10.5815 Debug File Target[file]
2018-09-20 19:26:10.5815 Debug Rules:
2018-09-20 19:26:10.5815 Debug logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] appendTo: [ file ]
2018-09-20 19:26:10.5815 Debug --- End of NLog configuration dump ---
2018-09-20 19:26:10.5815 Info Found 23 configuration items
2018-09-20 19:26:10.6105 Debug Watching path 'C:\inetpub\wwwroot\Test' filter 'NLog.config' for changes.
2018-09-20 19:26:10.6115 Info Configuration initialized.
2018-09-20 19:26:10.6115 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.5.7.7958. Product version: 4.5.7.
2018-09-20 19:26:10.6115 Debug Targets for Data.Repositories.CommonRepository by level:
2018-09-20 19:26:10.6115 Debug Trace => file
2018-09-20 19:26:10.6115 Debug Debug => file
2018-09-20 19:26:10.6115 Debug Info => file
2018-09-20 19:26:10.6115 Debug Warn => file
2018-09-20 19:26:10.6115 Debug Error => file
2018-09-20 19:26:10.6115 Debug Fatal => file
2018-09-20 19:26:10.6405 Debug Targets for Emailer.EmailManager by level:
2018-09-20 19:26:10.6405 Debug Trace => file
2018-09-20 19:26:10.6405 Debug Debug => file
2018-09-20 19:26:10.6405 Debug Info => file
2018-09-20 19:26:10.6405 Debug Warn => file
2018-09-20 19:26:10.6405 Debug Error => file
2018-09-20 19:26:10.6405 Debug Fatal => file
2018-09-20 19:26:10.6435 Debug Targets for UI.Controllers.BaseController by level:
2018-09-20 19:26:10.6435 Debug Trace => file
2018-09-20 19:26:10.6435 Debug Debug => file
2018-09-20 19:26:10.6435 Debug Info => file
2018-09-20 19:26:10.6435 Debug Warn => file
2018-09-20 19:26:10.6435 Debug Error => file
2018-09-20 19:26:10.6435 Debug Fatal => file
2018-09-20 19:26:10.7735 Debug FileTarget(Name=file): Process file 'C:\temp\logs\ErrorLogFile.log' on startup
2018-09-20 19:26:10.7735 Debug Creating file appender: C:\temp\logs\ErrorLogFile.log
MVC 5 project. .Net 4.5.2. NLog version 4.5.7
I also use SignalR in the project. The log folder has modify rights for IIS_IUSRS.
Any help appreciated.
In my case, It turned out Application_Error do not get called in server environment. implementing OnException method and logging in the method solved the issue.
I post my answer https://github.com/NLog/NLog/issues/2924
Related
I try to migrate my application from log4j to log4j2. According to official manual I have org.apache.logging.log4j:log4j-1.2-api:2.17.1, org.apache.logging.log4j:log4j-api:2.17.1 and org.apache.logging.log4j:log4j-core:2.17.1 in my maven dependencies. I use log4j.properties file so I enabled compatibility mode (-Dlog4j1.compatibility=true). RollingFileAppender is configured in my log4j.properties file:
log4j.appender.A2=org.apache.log4j.RollingFileAppender
I get following error:
2022-02-16 21:04:20,396 main ERROR Unable to create Appender org.apache.log4j.RollingFileAppender due to ClassNotFoundException:org.apache.log4j.RollingFileAppender
According to official manual, RollingFileAppender is a supported component. I had log4j:log4j:1.2.17 dependency before migration to log4j2 and it contains org.apache.log4j.RollingFileAppender class. Unfortunately none of the log4j-1.2-api,log4j-api and log4j-core libraries contain this class. I do not understand why RollingFileAppender is not present in log4j bridge if manual says that RollingFileAppender is supported component. I have found similar question but without a satisfactory answer. How to migrate RollingFileAppender?
UPDATE
Here is my log4j.properties file:
log4j.rootCategory=INFO, A1, A2
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d%m%M %n
log4j.appender.A2.file=C:/projects/test/Log.log
#log4j.appender.A2.layout=org.apache.log4j.PatternLayout
#log4j.appender.A2.layout.ConversionPattern=[%d{dd.MM HH.mm.ss.SSS} %-5p %-20c{1}] : %m%n
#log4j.appender.A2.MaxFileSize=10000KB
#log4j.appender.A2.MaxBackupIndex=3
I added -Dlog4j2.debug=true. There are tons of logs. I chose interesting logs:
TRACE StatusLogger Trying to find [log4j.properties] using context class loader jdk.internal.loader.ClassLoaders$AppClassLoader#2437c6dc.
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger PluginManager 'Log4j Builder' found 18 plugins
DEBUG StatusLogger Parsing for [root] with value=[INFO, A1, A2].
DEBUG StatusLogger Level token is [INFO].
DEBUG StatusLogger Logger root level set to INFO
DEBUG StatusLogger Parsing appender named "A1".
DEBUG StatusLogger PluginManager 'Converter' found 47 plugins
DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false
DEBUG StatusLogger Adding appender named [A1] to loggerConfig [].
DEBUG StatusLogger Parsing appender named "A2".
WARN StatusLogger Unable to create File Appender, no file name provided
ERROR StatusLogger Unable to create Appender org.apache.log4j.RollingFileAppender due to ClassNotFoundException:org.apache.log4j.RollingFileAppender
DEBUG StatusLogger Appender named [A2] not found.
DEBUG StatusLogger Finished configuring.
The reason configuration fails is explained by the following message:
WARN StatusLogger Unable to create File Appender, no file name provided
This is caused by a compatibility problem between the Log4j 1.x bridge and the original Log4j 1.x: property names must start with a capital letter (cf. LOG4J2-3316). This will be solved in the next release.
Until then, you can use:
log4j.appender.A2.File=C:/projects/test/Log.log
(with a capital F).
I'm using NLOG with Linux + MONO ASP.NET WebService and I'm not able to log two different file.
ASMX file have two different methos and I would like to log in separate logging file
This is Nlog.config
...
<targets async="true">
<target name="sendMail" xsi:type="File" fileName="${logDirectory}/${shortdate}_sendMail_${level}.log"
layout="${time} ${uppercase:${level}} ${message}" />
<target name="checkMail" xsi:type="File" fileName="${logDirectory}/${shortdate}_checkMail_${level}.log"
layout="${time} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger name="sendMail" minlevel="Debug" writeTo="sendMail" />
<logger name="checkMail" minlevel="Debug" writeTo="checkMail" />
</rules>
</nlog>
This is the log file:
2020-04-20 01:42:39.6781 Debug LogFactory Flush with timeout=15 secs
2020-04-20 01:42:39.6907 Debug FileTarget(Name=sendMail_wrapped): Process file '/var/log/wsMail/2020-04-20_sendMail_Info.log' on startup
2020-04-20 01:42:39.6907 Debug FileTarget(Name=checkMail_wrapped): Process file '/var/log/wsMail/2020-04-20_checkMail_Info.log' on startup
2020-04-20 01:42:39.7058 Debug Creating file appender: /var/log/wsMail/2020-04-20_checkMail_Info.log
2020-04-20 01:42:39.7058 Debug Creating file appender: /var/log/wsMail/2020-04-20_sendMail_Info.log
2020-04-20 01:42:39.7442 Debug Flush completed
2020-04-20 01:42:39.7442 Info Shutdown() called. Logger closing...
2020-04-20 01:42:39.7442 Info Closing old configuration.
2020-04-20 01:42:39.7442 Debug LogFactory Flush with timeout=15 secs
2020-04-20 01:42:39.7442 Debug Flush completed
2020-04-20 01:42:39.7442 Debug Closing logging configuration...
2020-04-20 01:42:39.7490 Debug Closing target 'AsyncWrapper Target[sendMail](File Target[sendMail_wrapped])'.
2020-04-20 01:42:39.7533 Debug Closed target 'AsyncWrapper Target[sendMail](File Target[sendMail_wrapped])'.
2020-04-20 01:42:39.7533 Debug Closing target 'File Target[sendMail_wrapped]'.
2020-04-20 01:42:39.7585 Debug FileAppender Closing Invalidate - /var/log/wsMail/2020-04-20_sendMail_Info.log
2020-04-20 01:42:39.7620 Debug Closed target 'File Target[sendMail_wrapped]'.
2020-04-20 01:42:39.7620 Debug Closing target 'AsyncWrapper Target[checkMail](File Target[checkMail_wrapped])'.
2020-04-20 01:42:39.7620 Debug Closed target 'AsyncWrapper Target[checkMail](File Target[checkMail_wrapped])'.
2020-04-20 01:42:39.7639 Debug Closing target 'File Target[checkMail_wrapped]'.
2020-04-20 01:42:39.7639 Debug FileAppender Closing Invalidate - /var/log/wsMail/2020-04-20_checkMail_Info.log
2020-04-20 01:42:39.7639 Debug Closed target 'File Target[checkMail_wrapped]'.
2020-04-20 01:42:39.7639 Debug Finished closing logging configuration.
2020-04-20 01:42:39.7639 Debug Stopping file watching for path '/var/www/wsMail' filter 'NLog.config'
2020-04-20 01:42:39.7775 Debug Targets not configured for logger: sendMail
2020-04-20 01:42:39.7775 Debug Targets not configured for logger: checkMail
2020-04-20 01:42:39.7775 Info Logger has been closed down.
This is the code in VS2019
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>
Public Class wsMail
Inherits System.Web.Services.WebService
Public Credentials As AuthHeader
Public smLog As NLog.Logger = LogManager.GetLogger("sendMail")
Public cmLog As NLog.Logger = LogManager.GetLogger("checkMail")
Public messageID As String = Nothing
Public retMsg As String = Nothing
Public retRemoteMsg As String = Nothing
......
cmLog.Error("|" & idCustomer & "|" & remoteIP & "|wrong Password|")
smLog.Debug("- v1.0.1 |" & idCustomer & "|" & remoteIP & "|" & ex.Message & ex.StackTrace & "|")
Any help is appreciated, thanks a lot
I would like to have log4j (v1.2.17) send ...
... to fileAppender
INFO (and above) from all packages
... to console
if from "my.project" then INFO (and above)
else WARN (and above) for all other packages
How can I do this? I'd prefer a properties file if possible, but would switch to XML if necessary.
I've tried combinations of loggers, non-additivity, Threshold, & LevelMatchFilter but can't figure it out.
This can definitely be done. Below you will find some example code and log4j properties that will do what you want.
Here is the first example class:
package my;
import org.apache.log4j.Logger;
public class Example1 {
private static final Logger logger = Logger.getLogger(Example1.class);
public static void main(String[] args){
logger.debug("debug from my.Example1 - should display nowhere!");
logger.info("info from my.Example1 - should display in log file only!");
logger.warn("warning from my.Example1 - should display in console and log file!");
logger.error("error from my.Example1 - should display in console and log file!");
logger.fatal("fatal from my.Example1 - should display in console and log file!");
}
}
Here is the second example class:
package my.project;
import org.apache.log4j.Logger;
public class Example2 {
private static final Logger logger = Logger.getLogger(Example2.class);
public static void main(String[] args){
logger.debug("debug from my.project.Example2 - should display nowhere!");
logger.info("info from my.project.Example2 - should display in console and log file!");
logger.warn("warning from my.project.Example2 - should display in console and log file!");
logger.error("error from my.project.Example2 - should display in console and log file!");
logger.fatal("fatal from my.project.Example2 - should display in console and log file!");
}
}
Here is the log4j.properties file:
# The root logger will accept INFO messages or higher and send them to the log file
# and to a console appender that filters out any messages below WARN level
log4j.rootLogger=INFO, R, warnStdout
# Configure a console appender that will be used for messages of any level
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#This will be used to print WARN level or higher messages to console
log4j.appender.warnStdout=org.apache.log4j.ConsoleAppender
log4j.appender.warnStdout.layout=org.apache.log4j.PatternLayout
log4j.appender.warnStdout.Threshold=WARN
# Pattern to output the caller's file name and line number.
log4j.appender.warnStdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=test.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# Classes in the my.project package will accept messages of INFO level or higher
# and send those messages to the console and to the log file
log4j.logger.my.project=INFO, stdout, R
# Need to set additivity to false or else both the my.project and root loggers
# will accept messages from classes in package my.project
log4j.additivity.my.project=false
Here is the console output after running Example1 followed immediately by Example2:
WARN [main] (Example1.java:11) - warning from my.Example1 - should display in console and log file!
ERROR [main] (Example1.java:12) - error from my.Example1 - should display in console and log file!
FATAL [main] (Example1.java:13) - fatal from my.Example1 - should display in console and log file!
INFO [main] (Example2.java:11) - info from my.project.Example2 - should display in console and log file!
WARN [main] (Example2.java:12) - warning from my.project.Example2 - should display in console and log file!
ERROR [main] (Example2.java:13) - error from my.project.Example2 - should display in console and log file!
FATAL [main] (Example2.java:14) - fatal from my.project.Example2 - should display in console and log file!
Here is the test.log file content after running Example1 followed by Example2:
INFO main my.Example1 - info from my.Example1 - should display in log file only!
WARN main my.Example1 - warning from my.Example1 - should display in console and log file!
ERROR main my.Example1 - error from my.Example1 - should display in console and log file!
FATAL main my.Example1 - fatal from my.Example1 - should display in console and log file!
INFO main my.project.Example2 - info from my.project.Example2 - should display in console and log file!
WARN main my.project.Example2 - warning from my.project.Example2 - should display in console and log file!
ERROR main my.project.Example2 - error from my.project.Example2 - should display in console and log file!
FATAL main my.project.Example2 - fatal from my.project.Example2 - should display in console and log file!
How do I silence the debug logging in http componenets?
09:23:22.145 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: default
09:23:22.145 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
09:23:22.145 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://example.org:80][total kept alive: 1; route allocated: 1 of 100; total allocated: 1 of 200]
09:23:22.145 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://diagnostics-uat.corp.apple.com:80][total kept alive: 0; route allocated: 1 of 100; total allocated: 1 of 200]
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request POST /evt/detect HTTP/1.1
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
09:23:22.145 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /evt/detect HTTP/1.1
09:23:22.145 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 92
I have tried a code based solution:
static {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "warn");
}
public static void main(String[] args) throws Exception {
And this log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
log4j.logger.org.apache.http=WARN
log4j.logger.org.apache.http.wire=WARN
But neither have worked. Help is appreciated, thanks!
You settings are correct possible causes may be another log4j config file is in the classpath and overriding your config. So I will recommended this.
- Ensure you log4j.properties file is in classpath . Use below command to see open files by a process (assuming unix)
lsof -p java-process-pid | grep log4j
Search for log4j.properties in your runtime folders and get rid of all duplicate files (if possible) - otherwise change them all.
Search for log4j.xml and do the same.
Sometimes I have seen third party jars that ship with a log4j.xml or log4j.properties so in worst case check for all jars using below command
jar -tvf a.jar | grep log4j
Programmatically, it can be set silent with:
(Apache HttpComponents 4.3.5)
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
...
Logger.getLogger("org.apache.http") .setLevel(Level.WARN);
Logger.getLogger("org.apache.http.wire") .setLevel(Level.WARN);
Logger.getLogger("org.apache.http.headers").setLevel(Level.WARN);
When I'm performing this line in my class:
pages.get(i).convertToImage(IMAGE_TYPE, RESOLUTION);
... I receive a lot of the following messages:
2014-06-11 17:01:42,476 DEBUG [org.apache.pdfbox.util.PDFStreamEngine] (DefaultQuartzScheduler_Worker-10) processing substream token: PDFOperator{Tj}
2014-06-11 17:01:42,494 DEBUG [org.apache.pdfbox.util.PDFStreamEngine] (DefaultQuartzScheduler_Worker-10) processing substream token: PDFOperator{ET}
2014-06-11 17:01:42,494 DEBUG [org.apache.pdfbox.util.PDFStreamEngine] (DefaultQuartzScheduler_Worker-10) processing substream token: PDFOperator{Q}
Nevertheless, the root logger in my jboss-logging.xml has been put to INFO. When I explicitly put this in my XML config:
<logger category="org.apache.pdfbox.util.PDFStreamEngine">
<level name="INFO"/>
</logger>
... it is still putting DEBUG statements in my LOG file.
Any help?