2021年3月2日 星期二

Serial Log - Write to AWS Setting

            Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                .Enrich.WithFunction("SystemUserName", () => Environment.UserName)
                .Enrich.WithFunction("SystemName", () => sysetmName)
                .Enrich.WithFunction("OSVersion", () => Environment.OSVersion.VersionString)
                .Enrich.WithFunction("CurrentManagedThreadId", () => Environment.CurrentManagedThreadId.ToString())
                .Enrich.WithFunction("CurrentTimeZone", () => TimeZone.CurrentTimeZone.StandardName)
                // 時間戳
                .Enrich.WithFunction("TimeStamp", () => {
                    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區
                    return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒數
                })
                .Enrich.WithFunction("LogGuid", () => Guid.NewGuid().ToString("N"))
                .WriteTo.AmazonS3(
                    new JsonFormatter(),
                    "log.log",
                    "logforservice",
                    Amazon.RegionEndpoint.APNortheast1,
                    "KeyId",
                    "Key",
                    fileSizeLimitBytes: 10,
                    autoUploadEvents: true,
                    rollingInterval: Serilog.Sinks.AmazonS3.RollingInterval.Minute,
                    bucketPath: $"{sysetmName}/{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}/{DateTime.Now.Hour}"
                )
                .CreateLogger();

2021年3月1日 星期一

NLOG Setting - write to Sql,File

 <?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">


  <variable name="myvar" value="myvalue"/>


  <targets>

    <target name="MessageLogFile" xsi:type="File" fileName="C://logs/CampaignFlow/SendMessageLog_${shortdate}.log" 

            layout="${longdate} | ${level:uppercase=true} |  ${message} ${newline}" />


    <target name="coloredConsole" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="false"

            layout="${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${message}" >

    <highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" />

    <highlight-row condition="level == LogLevel.Info" foregroundColor="Gray" />

    <highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />

    <highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />

    <highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" />

    </target>

    <target name="tracelogguid" xsi:type="database" connectionstring="${dbconnectionstring}" commandtext="insert into [dbo].[tracelog] &#xd;&#xa;     ([controller] ,[action] ,[request] ,[response] ,[createtime] ,[issuccess] ,[requestid] ,[exceptionresult],[errorcode],[stringkeyword],[intkeyword],[guidkeyword]) &#xd;&#xa;     values (@controller, @action, @request, @response, @createtime, @issuccess, @requestid, @exceptionresult,@errorcode,@stringkeyword,@intkeyword,@guidkeyword);">

<parameter name="@controller" layout="${event-properties:item=controller}" />

<parameter name="@action" layout="${event-properties:item=action}" />

     <parameter name="@request" layout="${event-properties:item=request}" />

<parameter name="@response" layout="${event-properties:item=response}" />

<parameter name="@createtime" layout="${event-properties:item=createtime}" />

<parameter name="@issuccess" layout="${event-properties:item=issuccess}" />

<parameter name="@requestid" layout="${event-properties:item=requestid}" />

<parameter name="@request" layout="${event-properties:item=request}" />

<parameter name="@errorcode" layout="${event-properties:item=errorcode}" />

<parameter name="@exceptionresult" layout="${exception:tostring}" />

<parameter name="@stringkeyword" layout="${event-properties:item=stringkeyword}" />

<parameter name="@intkeyword" layout="${event-properties:item=intkeyword}" />

<parameter name="@guidkeyword" layout="${event-properties:item=guidkeyword}" />

</target>


  </targets>


  <rules>

   <logger name="*" minlevel="Trace" writeTo="tracelogguid" />

    <logger name="*" levels="Trace,Debug,Warn" writeTo="MessageLogFile,coloredConsole" />

  </rules>

</nlog>