Giter VIP home page Giter VIP logo

nlog.targets.tencentcls's Introduction

NLog.Targets.TencentCls

腾讯日志服务(TencentCls)结合Nlog,配置NlogTarget 实现日志自动上传到CLs

TencentCloudSDK.Cls未实现日志上传功能,通过扩展方法实现UploadLog方法

TestCls/Program.Main()

            ClsClient client = new ClsClient(new TencentCloud.Common.Credential { SecretId = SECRET_ID, SecretKey = SECRET_KEY }, region);

            var headers = new Dictionary<String, String>
            {
                { X_CLS_TOPIC_ID, topicId },
                { X_CLS_HASH_KEY, "" }
            };
            // body lz4 压缩
            //headers.Add(X_CLS_COMPRESS_TYPE, LZ_4);
            
            Log log = new Log();
            var content = new Log.Types.Content{ Key = "reqId", Value = "88888888888888" };
            log.Contents.Add(content);
            log.Time = DateTime.UtcNow.ToTimestamp().Seconds;

            LogGroup logGroup = new LogGroup();
            logGroup.Logs.Add(log);

            LogGroupList logGroupList = new LogGroupList();
            logGroupList.LogGroupList_.Add(logGroup);
            var body = logGroupList.ToByteArray();

            var res = client.CallOctetStream("UploadLog", Service, headers, body).Result;

NlogTarget 配置

参考TestWebApi

<nlog>
  <extensions>
    <add assembly="NLog.Targets.TencentCls"/>
  </extensions>
  <targets>
     <!-- cls Target  -->
	  <target xsi:type="TencentCls" name="CLS" SecretId="XXXX" 
			  SecretKey="XXXX" Region="ap-shanghai" TopicId="8e8edc72-bf58-4d8e-8240-892494981266">
		  <layout xsi:type="JsonLayout" includeEventProperties="Boolean" excludeProperties="Comma-separated list (string)">
			  <attribute name="time" layout="${longdate}" />
			  <attribute name="level" layout="${level:upperCase=true}"/>
			  <attribute name="callsite" layout="${callsite}" />
			  <attribute name="message" layout="${message}" />
		  </layout>
	  </target>
  </targets
  <rules>
     <logger name="TestWebApi.*" minlevel="Trace" writeTo="CLS" />
  </rules>

nlog.targets.tencentcls's People

Contributors

yinsenzhang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.