A .NET Core NLog target to send log message to Aliyun Log Service.
Install-Package NLog.Targets.Aliyun
-
Create a Log Service project and logstore
-
Enable RAM access control and create an AccessKey
-
Add NLog.Targets.Aliyun as an extension to your nlog.config
-
Configure endpoint, project, accessKeyId and accessKey in the nlog.config
-
Specify the logstore you want the log post to
e.g., set logStore="${lowercase:${aspnet-environment}}", the log will send to development, staging or production logstore depends on the environment, some initialization logs will be dropped as environment not set yet
- name - Specify a name, but make sure you use the same name in logger rules section
- endpoint - You can find it in the home screnn of your log service project. Use external endpoint if your application is not deployed within Aliyun
- project - The log service project name you specified
- accessKeyId - The RAM access key id
- accessKey - The RAM access key
- logStore - Support layout rendering, the log service log store name you specified
- source - Support layout rendering, leave as empty send nothing to server while remove this attribute will send the IP address the application are running
- topic - Support layout rendering, leave as empty send nothing to server while remove this attribute will send the logger name
- layout - The message layout, this property will be ignored when context properties is set
- Add NLog.Targets.Aliyun Extension
<add assembly="NLog.Targets.Aliyun"/>
- Add Aliyun Target
<target xsi:type="Aliyun" name="aliyun"
endpoint="<your-endpoint>"
project="<your-project-name>"
accessKeyId="<your-access-key-id>"
accessKey="<your-access-key>"
logStore="<your-log-store-name>">
<contextProperty name="time" layout="${longdate}" />
<contextProperty name="level" layout="${level}" />
<contextProperty name="sequence" layout="${sequenceId}" />
<contextProperty name="message" layout="${message}" />
<contextProperty name="exception" layout="${exception:format=ToString,StackTrace}" includeEmptyValue="false" />
</target>
- Add Logger Rules
<logger name="*" minlevel="Debug" writeTo="aliyun" />
See a full example in the demo project.