ASP.NET Core 中使用 GrayLog 记录日志
使用 UDP 协议发送日志 graylog2-server 服务端格式化日志再存储
Ubuntu 安装服务端
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install apt-transport-https openjdk-8-jre-headless uuid-runtime pwgensudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt-get update
sudo apt-get install -y mongodb-orgsudo systemctl daemon-reload
sudo systemctl enable mongod.service
sudo systemctl restart mongod.service
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/5.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-5.x.list
sudo apt-get update && sudo apt-get install elasticsearch
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl restart elasticsearch.servicewget https://packages.graylog2.org/repo/packages/graylog-2.4-repository_latest.deb
sudo dpkg -i graylog-2.4-repository_latest.deb
sudo apt-get update && sudo apt-get install graylog-serversudo systemctl daemon-reload
sudo systemctl enable graylog-server.service
sudo systemctl start graylog-server.servicedocker :http://docs.graylog.org/en/2.4/pages/installation/docker.html
ASP.NET CORE 2 集成
nuget install Gelf.Extensions.Logging
public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureLogging((context, builder) => { // Read GelfLoggerOptions from appsettings.json. builder.Services.Configure<GelfLoggerOptions>(context.Configuration.GetSection("Graylog")); // Optionally configure GelfLoggerOptions further. builder.Services.PostConfigure<GelfLoggerOptions>(options => options.AdditionalFields["machine_name"] = Environment.MachineName); // Read Logging settings from appsettings.json and add providers. builder.AddConfiguration(context.Configuration.GetSection("Logging")) .AddConsole() .AddDebug() .AddGelf(); }) .Build(); }
非 .net core 环境也可以使用 NLog 集成
<?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="c:\temp\nlog-internal.log"> <extensions> <add assembly="EasyGelf.NLog"/> </extensions> <targets async="true"> <!-- https://github.com/NLog/NLog/wiki/File-Target ${aspnet-session:Variable=SESSION.UserName:EvaluateAsNestedProperties=true}:${aspnet-session:Variable=SESSION.HotelCd:EvaluateAsNestedProperties=true}:${aspnet-session:Variable=SESSION.HotelName:EvaluateAsNestedProperties=true} ${aspnet-request-url:IncludeQueryString=true} --> <!-- Write events to a file with the date in the filename. --> <target name="f" xsi:type="File" fileName="${basedir}/logs/${shortdate}/${level}.log" layout="${longdate} ${aspnet-MVC-Controller} ${aspnet-MVC-Action} ${threadid:padding=2} ${level:uppercase=true:padding=2} ${logger:shortName=true} ${message} ${exception:format=tostring}" archiveFileName="${shortdate}/log.{#}.txt" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="7" concurrentWrites="true" createDirs="true" autoFlush="true" encoding="utf-8"/> <!-- Write events to the graylog. --> <target name="GelfUdp" xsi:type="GelfUdp" facility="ProjectName" remoteAddress="192.168.1.1" remotePort="12201" layout="${message}"> <parameter name="level" layout="${level}" /> <parameter name="thread_id" layout="${threadid}" /> <parameter name="controller" layout="${aspnet-MVC-Controller}" /> <parameter name="action" layout="${aspnet-MVC-Action}" /> <parameter name="create_time" layout="${longdate}" /> </target> </targets> <rules> <!-- add your logging rules here --> <!-- Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f" --> <logger name="*" minlevel="Debug" writeTo="f" /> <!--<logger name="*" minlevel="Info" writeTo="GelfUdp" />--> </rules> </nlog>
REFER:
https://github.com/Graylog2/graylog2-server
https://github.com/aspnet/Logging
https://github.com/mattwcole/gelf-extensions-logging
https://github.com/GokGokalp/NLog.Web.AspNetCore.Targets.Gelf
https://blog.takipi.com/how-to-choose-the-right-log-management-tool/
http://www.cnblogs.com/RainingNight/p/asp-net-core-logging-elasticsearch-kibana.html
Elasticsearch 疑难解惑
http://www.cnblogs.com/clonen/p/8116386.html