Menambahkan header yang berisi id korelasi

Artikel ini menunjukkan contoh kebijakan manajemen Azure API yang menunjukkan cara menambahkan header yang berisi id korelasi ke permintaan masuk. Untuk mengatur atau mengedit kode kebijakan, ikuti langkah-langkah yang dijelaskan dalam Mengatur atau mengedit kebijakan. Untuk melihat contoh lain, lihat sampel kebijakan.


Tempelkan kode ke blok masuk.

<!-- The policy defined in this file demonstrates how to add a header containing a correlation id to the inbound request. -->
<!-- The id could be used to correlate requests forwarded by Azure API Management to requests in your backend. -->
<!-- The snippet uses a COMB GUID as an id value for efficient query performance. -->

<!-- NOTE: If COMB format is not needed, context.RequestId should be used as a value of correlation id. -->
<!-- context.RequestId is unique for each request and is stored as part of gateway log records. -->

<!-- Copy the following snippet into the inbound section. Applying this snippet at the global level will ensure that correlation id is added to all requests. -->

        <base />
        <set-header name="correlationid" exists-action="skip">
                var guidBinary = new byte[16];
                Array.Copy(Guid.NewGuid().ToByteArray(), 0, guidBinary, 0, 10);
                long time = DateTime.Now.Ticks;
                byte[] bytes = new byte[6];
                       bytes[5] = (byte)(time >> 40);
                       bytes[4] = (byte)(time >> 32);
                       bytes[3] = (byte)(time >> 24);
                       bytes[2] = (byte)(time >> 16);
                       bytes[1] = (byte)(time >> 8);
                       bytes[0] = (byte)(time);
                Array.Copy(bytes, 0, guidBinary, 10, 6);
                return new Guid(guidBinary).ToString();
        <base />
        <base />
        <base />

Langkah berikutnya

Pelajari lebih lanjut kebijakan APIM: