forked from GoogleCloudPlatform/dotnet-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebApiConfig.cs
80 lines (75 loc) · 3.13 KB
/
WebApiConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright(c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Routing;
using log4net;
using System.Configuration;
using System.Xml;
namespace GoogleCloudSamples
{
public static class WebApiConfig
{
// [START sample]
/// <summary>
/// Simple HTTP Handler that demonstrates logging using Log4Net
/// and the Stackdriver Logging API.
/// </summary>
public class HelloWorldHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
string response = "";
// Check to ensure that projectId has been changed from placeholder value.
var section = (XmlElement)ConfigurationManager.GetSection("log4net");
XmlElement element =
(XmlElement)section.GetElementsByTagName("projectId").Item(0);
string projectId = element.Attributes["value"].Value;
if (projectId != "YOUR-PROJECT-ID")
{
// [START log4net_log_entry]
// Retrieve a logger for this context.
ILog log = LogManager.GetLogger(typeof(WebApiConfig));
// Log some information to Google Stackdriver Logging.
log.Info("Hello World.");
// [END log4net_log_entry]
// Pause for 5 seconds to ensure log entry is completed
Thread.Sleep(5000);
response = $"Log Entry created in project: {projectId}";
}
else
{
response = "Update Web.config and replace YOUR-PROJECT" +
"-ID with your project id, and recompile.";
}
return Task.FromResult(new HttpResponseMessage()
{
Content = new ByteArrayContent(Encoding.UTF8.GetBytes(response))
});
}
};
public static void Register(HttpConfiguration config)
{
var emptyDictionary = new HttpRouteValueDictionary();
// Add our one HttpMessageHandler to the root path.
config.Routes.MapHttpRoute("index", "", emptyDictionary, emptyDictionary,
new HelloWorldHandler());
}
// [END sample]
}
}