The front-end time wrote an essay log4net. NOSQL +elasticsearch implements logging , because of project reasons need to integrate log root Java platform colleague integration using Logstash+kibana+elasticsearch+redis structure to achieve log statistics analysis, Therefore, a component that outputs Log4net logs to Redis is required. Did not find the ready-made, do it yourself. Reference to the log4net. NOSQL Code.
Redis's C # client uses Servicestackredis, and JSON serialization uses Restsharp. the code structure is as follows:
JsonLayout.cs Code:
Public classJsonlayout:layoutskeleton { Public ReadOnly stringHostName; Private ReadOnlyItexttransform _transform; Public stringLogType {Get;Set; } Public stringAppName {Get;Set; } Publicjsonlayout ():Base() {HostName=Dns.gethostname (); _transform=texttransform.instance; } Public Override stringContentType {Get{return "Application/json"; } } Public Override voidactivateoptions () {// Nothing } Public Override voidFormat (TextWriter writer, loggingevent loggingevent) {varinfo =loggingevent.locationinformation; varLoggingeventjson = _transform. Serialize (NewJsonlogmessage {Class_method=info. MethodName, Class_name=info. ClassName, Host_ip=HostName, Line_number=info. LineNumber, Log_level=LoggingEvent.Level.DisplayName, Log_message=loggingEvent.MessageObject.ToString (), Exception=Buildexceptionmessage (loggingevent), Log_time= LoggingEvent.TimeStamp.ToString ("YYYY-MM-DD HH:mm:ss"), Logger_name=Loggingevent.loggername, Run_time="0",//currently not availableThread_name =Loggingevent.threadname, Host=HostName, Log_type= This. LogType, App_name= This. AppName, Path="" }); Writer. Write (Loggingeventjson); } Privatejsonlogexception buildexceptionmessage (loggingevent loggingevent) {if(Loggingevent.exceptionobject = =NULL) return Newjsonlogexception {Exception_class ="", Exception_message ="", Exception_stacktrace ="" }; varException =Loggingevent.exceptionobject; return Newjsonlogexception {exception_class=exception. Source, Exception_message=exception. Message, Exception_stacktrace=exception. StackTrace}; } }
View Code
RedisAppender.cs
Public classRedisappender:appenderskeleton {Private volatilepooledredisclient _pooledredisclient; Private ReadOnly Object_padlock =New Object(); Public stringHost {Get;Set; } Public stringPort {Get;Set; } Public stringListId {Get;Set; } Public stringmaxpoolsize {Get;Set; } PublicRedisappender () {}Privatepooledredisclient Client {Get { if(_pooledredisclient! =NULL)return_pooledredisclient; Lock(_padlock) {if(_pooledredisclient = =NULL) {_pooledredisclient=NewPooledredisclient ( This. Host,int. Parse ( This. Port),int. Parse ( This. Maxpoolsize)); } } return_pooledredisclient; } } protected Override voidAppend (loggingevent loggingevent) {varSB =NewStringBuilder (); varwriter =NewStringWriter (SB); Base. Layout.format (writer, loggingevent); This. Client.additemtolistasync (ListId, writer. ToString ()); } }
View Code
PooledRedisClient.cs
Public classpooledredisclient {Private ReadOnly string_baseuri; Private ReadOnlyPooledredisclientmanager _clientmanager; /// <summary> ///instantiating a connection pool client/// </summary> /// <param name= "host" ></param> /// <param name= "Port" ></param> /// <param name= "Maxpoolsize" >default value of ten</param> PublicPooledredisclient (stringHostintPortintMaxpoolsize =Ten) {_baseuri=string. Format ("{0}:{1}", host, Port); varConfig =NewRedisclientmanagerconfig (); Config. Maxreadpoolsize=maxpoolsize; Config. Maxwritepoolsize=maxpoolsize; _clientmanager=NewPooledredisclientmanager (New string[] {_baseuri},New string[] {_baseuri}, config); } /// <summary> ///Asynchronous Logging/// </summary> /// <param name= "ListId" ></param> /// <param name= "Log" ></param> Public voidAdditemtolistasync (stringListId,stringlog) { //using task tasks to execute asynchronously varTask =NewTask (() = { Try { using(varClinet =_clientmanager.getclient ()) {clinet. Additemtolist (listId, log); } } Catch(Exception ex) {Trace.WriteLine (ex). Message); } }); Task. Start (); } }
View Code
A connection pool is used when the redisclient is connected, and a task is used to write the log asynchronously. This kind of writing does not know whether there is a problem?!
Configuration file
<?XML version= "1.0"?><log4net> <!--Redis Log Records - <Appendername= "Redisappender"type= "log4net." Redis.Appender.RedisAppender, Log4net. Redis "> <Hostvalue= "192.168.0.10" /> <Portvalue= "6379" /> <maxpoolsizevalue= "$" /> <ListIdvalue= "Logstash" /> <Layouttype= "log4net." Redis.layout.jsonlayout,log4net. Redis " > <LogTypevalue= "Iis_log" /> <AppNamevalue= "Demo" /> </Layout> </Appender> <Root> < Levelvalue= "All" /> <Appender-refref= "Redisappender" /> </Root></log4net>
View Code
The property configuration in layout and Appender is consistent with the property configuration name in the code, and the Log4net framework can read the configuration data and reflect it to the entity properties.
Code:
http://download.csdn.net/detail/zbl131/7702673
Reference article:
1. Quickly build real-time log query, collection and analysis system with Kibana and Logstash
http://storysky.blog.51cto.com/628458/1158707/
2. Introduction to using Servicestackredis link Redis
Http://www.cnblogs.com/daizhj/archive/2011/02/17/1956860.html
3.problems and corrections of Servicestack.redis
http://blog.csdn.net/susubuhui/article/details/8930417
4. Failover transformation of the Servicestack.redis connection pool
Http://www.cnblogs.com/smark/archive/2013/05/24/3096488.html