The errors are coming through fine in my file appender, but not in the sql appender.
Offending line:
int x = 0;
int y = 1 / x;
EDIT: apparently it doesn't log anything that's unhandled...
Maybe you should post a little more information (e.g. the log4net configuration)... based on what you write I assume the following:
You have an exception handler that calls log4net
Exceptions logged thus are processed correctly by a file appender
Logging of normal messages works fine for both appenders
I think that this means, there is something wrong with the configuration of the ado appender. Probably you try to pass the exception object and this somehow does not work correctly (type mismatch of sorts). Turning on internal debugging should indicated exactly what goes wrong.
Related
I have a global filter for exceptions. I want to create a logger. But in the filter, I can't see in which line or file the error occurred. Is it possible to know about it in the filter?
I want to do this because I don't want to add a logger to every part of the project. Any idea would be appreciated.
Every error, including Nest's HttpException class and child classes, should have a .stack property that tells where the error was thrown, what line, and what happened to get to that point. You can log that and then grok it as necessary to get the information you want
I can't be able to add console appender using logger.addAppender method with log4j-over-slf4j 1.7.x dependency. Moreover, I am unable to set target of that particular Console Appender(i.e., SYSTEM_OUT/SYSTEM_ERR).
I have initialized a console appender object and tried to push that reference into addAppender method by typecasting that reference to Appender. But in that case I am unable to set Target/WriterLocation(i.e., SYSTEM_OUT/SYSTEM_ERR) for console appender reference. I have used the below code snippet-
ConsoleAppender ca = new ConsoleAppender();
ca.setWriter(new OutputStreamWriter(System.out)); // this line is not compatible with log4j-over-slf4j jar
ca.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
logger.addAppender(ca);
Please help me to sort out this problem.
What you are doing doesn't make a lot of sense. log4j-over-slf4j will route calls like logger.debug(), logger.info(), etc to slf4j and then presumably to some other logging framework to be logged. Your code is trying to manipulate log4j 1 objects which won't be involved in logging since you are routing log events to SLF4J (which is why many of them aren't supported by log4j-over-slf4j).
In order to help you we would know what logging implementation you really want to use.
I'm in the process of writing an application that uses the WS object to make HTTP requests. However when making them (deliberately to a service that does not run) , I get a message from the inner Play! code:
15:06:29,613 ERROR ~ java.util.concurrent.ExecutionException: java.net.ConnectException: Connection refused to http://127.0.0.1:9091/
I traced this back to WSAsync.get() (https://github.com/playframework/play/blob/master/framework/src/play/libs/ws/WSAsync.java#L199):
public HttpResponse get() {
this.type = "GET";
sign();
try {
return new HttpAsyncResponse(prepare(prepareGet()).execute().get());
} catch (Exception e) {
Logger.error(e.toString());
throw new RuntimeException(e);
}
}
As this message is non-informational in my use case, I'd not like to show this error, so I decided to add a directive to my log4j.properties and restart the concerning application (Play! does not automatically reload it):
log4j.rootLogger=ERROR, Rolling
log4j.logger.play=INFO
log4j.logger.play.libs.ws=FATAL
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
log4j.appender.Rolling.File=logs/play-as.log
log4j.appender.Rolling.MaxFileSize=100KB
log4j.appender.Rolling.MaxBackupIndex=100
log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n
However I still receive the messages in my logs. I don't really understand because it should run in the same classpath. Any guesses on this?
Play always log under one category "play". They use always Logger..., so your approach can't work. There was some discussion about this issue, but the developer of play prefer only one category for simplicity.
Add
log4j.debug = TRUE
That enables debugging for log4j: You'll see which options are parsed. If you don't see anything, then the properties aren't loaded. Maybe there is a typo or the file is in the wrong place.
Note: You can specify the class name as well to avoid to suppress too much (i.e. log4j.logger.play.libs.ws.WSAsync=FATAL)
Also note: You should really use the XML version to configure; using properties is so error prone...
How can I setup log4net to write to the Output window of MsTest?
The only function I've been able to output to that window with is TestContext.WriteLine, but I do not know if there is an appender that can do that. Trace and Console outputs do not work.
I created a custom appender that I could pass the TestContext to, and use the WriteLine method on that for logging.
To setup log4net to use the TestContext, I created a custom appender (NUnitLogAppender.cs):
https://gist.github.com/dropthemic/f360c389c35758bbb94e67b3aee09157
This is based off Console appender. To write to TestContext or TestContext.Progress, set the "Target" in NUnitLogAppender class.
I have select few places in my application where I'd like to always log values. I could simply use Log.Info() and leave it at that, but I'd prefer a solution that can't be disabled by an accidental change to the level configuration. In this case, as long is log4net is not disabled, I want these log statements to fire.
What's the best approach?
Just looking at some information it looks like one option is to create a custom level with a value set above Emergency, but I don't know if that's a brutally awful hack with side effects I'm not realizing or a legitimate option. I couldn't find any clear guidance in the documentation.
I am not a log4net expert, but something like this might do what you want:
This code will get a named logger from the LogManager and will programmatically set its level to "ALL". If you retrieve this logger later in your code, it will always log, even if the log level is set to OFF in the config file.
To test, set the root log level to "OFF" in the config file, then use the code below:
log4net.ILog log = log4net.LogManager.GetLogger("abc");
log.Info("this won't log because root logger is OFF");
//Reset the level
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger;
l.Level = l.Hierarchy.LevelMap["ALL"];
//Try to log again
log.Info("this will log because we just reset abc's level to ALL");
I tested it and it does seem to work.
I found this information here and here.