Anyone know if it's possible to send GELF AMQP messages straight into Graylog with gelf4net?
I can see old posts about using the old graylog2 radios to subscribe to rabbitmq or similar brokers. But then found this post which says it's now built into graylog v1.
The gelf4net setup looks simple enough - but how do I setup the GELF AMQP input in Graylog?
Uff 3 years later...
I don't search for gelf4net questions in StackOverflow :/ since there are no many questions I usually answers them in github..
This is the log4net.config
<log4net>
<appender name="GelfAmqpAppender" type="Gelf4Net.Appender.GelfAmqpAppender, Gelf4Net.AmqpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="5672" />
<username value="guest" />
<password value="guest" />
<virtualHost value="/" />
<exchange value="log-messages" />
<key value="#" />
<layout type="Gelf4Net.Layout.GelfLayout, Gelf4Net.AmqpAppender">
<param name="AdditionalFields" value="app:GelfAmqpAppender,version:1.0,Level:%level" />
<param name="Facility" value="RandomPhrases" />
<param name="IncludeLocationInformation" value="true" />
<!-- Sets the full_message and short_message to the specified pattern-->
<!--<param name="ConversionPattern" value="[%t] %c{1} - %m" />-->
</layout>
</appender>
<appender name="AsyncGelfAmqpAppender" type="Gelf4Net.Appender.AsyncGelfAmqpAppender, Gelf4Net.AmqpAppender">
<remoteAddress value="127.0.0.1" />
<remotePort value="5672" />
<username value="guest" />
<password value="guest" />
<virtualHost value="/" />
<exchange value="log-messages" />
<key value="#" />
<layout type="Gelf4Net.Layout.GelfLayout, Gelf4Net.AmqpAppender">
<param name="AdditionalFields" value="app:AsyncGelfAmqpAppender,version:1.0,Level:%level" />
<param name="Facility" value="RandomPhrases" />
<param name="IncludeLocationInformation" value="true" />
<!-- Sets the full_message and short_message to the specified pattern-->
<!--<param name="ConversionPattern" value="[%t] %c{1} - %m" />-->
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="GelfAmqpAppender" />
<appender-ref ref="AsyncGelfAmqpAppender" />
</root>
</log4net>
the property <exchange value="log-messages" /> you should create the Exchange and a Queue named log-messages in the RabbitMQ Console
And here is the Input configuration in Graylog
This configuration is my dev environment, you should go deeper in RabbitMQ and Graylog for an optimal configuration
Related
I've been trying to find documentation and guidance on how to use a separate log file in ASP.NET Boilerplate MVC.
It's straight forward to inject ILogger and push messages into the default Log.txt file, however I need a separate log file to record a lot of batch job messages and keep things tidy.
I can't find any methods to use another log from the Log4Net.config file. Can anyone advise on the correct code / configuration?
So I found the answer by chance in looking at the Hangfire documentation, where it was shown you can configure Hangfire messages to be sent to another log file by getting Log4Net to listen for the Hangfire name and route based on that.
With the following additional configuration in Log4Net.config I now have Log messages created normally within MyClass sent to the MyClass.txt log file:
<appender name="RollingFileAppender_MyClass" type="log4net.Appender.RollingFileAppender" >
<file value="App_Data/Logs/MyClass.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="32" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<logger additivity="false" name="MyNamespace.MyClass">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender_MyClass" />
</logger>
And for completeness, it's called in the standard way documented within the AspNet Boilerplate documentation:
https://aspnetboilerplate.com/Pages/Documents/Logging
Way-1:
In this sample, the default log file is App_Data/Logs/Logs.txt.
But when I use the logger in the namespace MySite.Web.Controllers.PaymentController, the logs will be written in App_Data/Logs/PaymentLogs.txt
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="App_Data/Logs/Logs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender_PaymentController" type="log4net.Appender.RollingFileAppender" >
<file value="App_Data/Logs/PaymentLogs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="32" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
<level value="DEBUG" />
</root>
<logger name="NHibernate">
<level value="WARN" />
</logger>
<logger name="MySite.Web.Controllers.PaymentController" additivity="false">
<appender-ref ref="RollingFileAppender_PaymentController" />
<level value="DEBUG" />
</logger>
</log4net>
Way-2:
You can get a specific logger by calling LogManager.GetLogger() with a appender name.
private static readonly ILog paymentLogger = LogManager.GetLogger("MySite.Web.Controllers.PaymentController");
I've got an appender that I thought should be configured to only send emails when logging at the ERROR level.
But I keep getting emails from the application which don't have any entries with the ERROR log level.
Is there something in this configuration that is "fooling" the appender into sending? Something to do with the lossy setting perhaps?
<appender name="SmtpErrorAppender" type="log4net.Appender.SmtpAppender">
<to value="myemail#somewhere.com"/>
<from value="someapp#config.com" />
<subject value="Service Error" />
<authentication value="None" />
<smtpHost value="127.0.0.1" />
<port value="25" />
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR" />
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline %-5level %date %message%newline%newline%newline" />
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingLogFileAppender"/>
<appender-ref ref="SmtpErrorAppender"/>
</root>
Set a log level filter within your appender.
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
I have configured log4net with a RollingLogFileAppender and a SmtpAppender, with the intention of logging the DEBUG level to the RollingLogFileAppender and FATAL only to the SmtpAppender:
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="test#test.com" />
<from value="test#test.com" />
<subject value="Fatal Error" />
<smtpHost value="smtp.test.com" />
<SMTPPort value="366"/>
<Username value="test#test.com"/>
<Password value="password"/>
<bufferSize value="1" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="FATAL"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="SmtpAppender" />
</root>
This works perfectly until I increase the bufferSize. When I do this, all levels are sent via email and the log4net.Core.LevelEvaluator seems to be ignored. I have also tried using LevelRangeFilter and LevelMatchFilter but with these configured I seem to get no emails at all.
The evaluator is not ignored, but it does not do what you expect: Your settings instruct the appender to put all log messages on a buffer and send an email only when a message with level FATAL is logged. If the buffer is full then the oldest messages are discarded (that is the lossy setting; without it you would also get an email as soon as the buffer is full).
If you want to filter the messages then you need to use a filter. For instance like this:
<filter type="log4net.Filter.LevelMatchFilter">
<acceptOnMatch value="true" />
<levelToMatch value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
I am not sure though if I would consider my mail appender like this since I would want to get notified immediately if my application has a problem that it needs to log it with level FATAL.
I am using log4j for logging. I have a scenario where I have to use seperate logging for different severity. For example, for package foo I have to print messages with severity ERROR in the console whereas I have to print messages with severity WARN in log file. How can I configure my log4j.xml for the same.
You may use LevelMatchFilter for your task. Also you should not forget to use another filter - DenyAllFilter - as the last filter in the chain of filters.
Your log4j.xml will look like this:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout" />
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="ERROR" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="error.log" />
<layout class="org.apache.log4j.SimpleLayout" />
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="WARN" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<logger name="foo">
<level value="WARN" />
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
</logger>
</log4j:configuration>
I'm using log4net for logging and sending e-mails. I need to send different e-mails with different configurations according to different processes. For example, when one of my methods finishes processing, it sends a mail to X, and when my service finishes successfully, it sends another mail to Y.
I think I need to use different appenders with filters, but I can't get to configure them properly.
I tried the following and tried to apply a StringMatchFilter so that messages containing "mandatory" will not be delivered to Y and messages containing "successfully" will not be delivered to X, but with no luck. I am also not sure whether this kind of filtering is the right way to do this kind of configuration.
<log4net>
<appender name="ManFieldMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="successfully" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="x#xxx.com" />
<from value="aaa#aaa.com" />
<subject value="Products not made online" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<appender name="ServiceFinishedMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="mandatory" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="y#yyy.com" />
<from value="bbb#bbb.com" />
<subject value="Product Publish Service has successfully finished operating" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<root>
<appender-ref ref="ManFieldMailAppender" />
<appender-ref ref="ServiceFinishedMailAppender"/>
</root>
</log4net>
What do you think the problem is?
Thank you.
First, evaluator tells buffering appenders when to flush. From the look of your configuration I don't think you're buffering. (so you can get rid of the bufferSize and lossy parameters, too). I see your appender filter chains have the required DenyAllFilters. But, your root logger configuration is showing no levels, try adding
<root>
<level value="ALL" />
<!-- appender-refs as normal -->
</root>
In general, I wouldn't make the decision to log to a particular appender based on the message text. It looks like you have a high-level service class for one of the operations. If you have the same thing for the other messages that need to be sent, and you're naming your loggers by typename, you can specify an appender per logger.
<logger name="MyCompany.Product.Whatever">
<level value="WARN" />
<appender name="ManFieldMailAppender" type="...">
<!-- the rest of the configuration, without filtering goes here -->
</appender>
</logger>
<logger name="MyCompany.Services.Publish">
<level value="WARN" />
<appender name="ServiceFinishedMailAppender" type="...">
<!-- the rest of the configuration, without filtering goes here -->
</appender>
</logger>