Skip to content

Commit

Permalink
Add a web api endpoint to response log data in json
Browse files Browse the repository at this point in the history
  • Loading branch information
kasravi committed Oct 24, 2016
1 parent 7a8148b commit f8840c9
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 7 deletions.
10 changes: 10 additions & 0 deletions Composite/Composite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\bin\Microsoft.Practices.ObjectBuilder.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NodaTime, Version=1.3.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1, processorArchitecture=MSIL">
<HintPath>..\packages\NodaTime.1.3.2\lib\net35-Client\NodaTime.dll</HintPath>
<Private>True</Private>
Expand All @@ -108,6 +112,7 @@
<Reference Include="System.EnterpriseServices" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
Expand All @@ -119,6 +124,10 @@
</Reference>
<Reference Include="System.Transactions" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -178,6 +187,7 @@
<Compile Include="C1Console\Workflow\FormsWorkflowExtensions.cs" />
<Compile Include="C1Console\Workflow\IFormsWorkflowExtension.cs" />
<Compile Include="Core\Extensions\DateTimeExtensionMethods.cs" />
<Compile Include="Core\WebClient\Logging\WebApi\LoggerController.cs" />
<Compile Include="Core\WebClient\Services\ConsoleMessageService\OpenSlideViewParams.cs" />
<Compile Include="Data\DataScopeServicesFacade.cs" />
<Compile Include="Data\DataServiceScopeManager.cs" />
Expand Down
107 changes: 107 additions & 0 deletions Composite/Core/WebClient/Logging/WebApi/LoggerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using Composite.Core.Logging;
using Newtonsoft.Json;


namespace Composite.Core.WebClient.Logging.WebApi
{
/// <summary>
/// Logger web api endpoint
/// </summary>
[RoutePrefix("Composite/api/Logger")]
public class LoggerController : ApiController
{
/// <summary>
/// Get method
/// </summary>
/// <returns></returns>
[Route("")]
public HttpResponseMessage Get()
{
var logRequestInfo = new LogRequestInfo()
{
Amount = "100",
DateFrom = DateTime.Now.AddDays(-1).ToLongDateString(),
DateTo = DateTime.Now.ToLongDateString(),
Severity = "Verbose"
};

var res = GetLogDatas(logRequestInfo);

return HttpResponseMessage(res);
}

/// <summary>
/// Get method
/// </summary>
/// <returns></returns>
[Route("GetDates")]

This comment has been minimized.

Copy link
@burningice2866

burningice2866 Oct 26, 2016

Contributor

Including Get as a part of the route isn't necessary in regards to web apis, since get should be inferred by the Http Method (ie. get, post, delete etc.).

public HttpResponseMessage GetDates()
{
var res = LogManager.GetLoggingDates();

return HttpResponseMessage(res);
}

/// <summary>
/// Post log info
/// </summary>
/// <param name="logRequestInfo"></param>
/// <returns></returns>
[Route("GetData")]
[HttpPost]

This comment has been minimized.

Copy link
@burningice2866

burningice2866 Oct 26, 2016

Contributor

An endpoint returning data shouldn't use the POST verb, that's for creating data. Use the querystring for filtering a get-request.

public HttpResponseMessage GetData([FromBody] LogRequestInfo logRequestInfo)

This comment has been minimized.

Copy link
@burningice2866

burningice2866 Oct 26, 2016

Contributor

The name of the endpoint should be just Data, not GetData, since Get is inferred by the Http Method.

{
var res = GetLogDatas(logRequestInfo);

return HttpResponseMessage(res);
}

private HttpResponseMessage HttpResponseMessage(object res)
{
string resJson = JsonConvert.SerializeObject(res);
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(resJson, Encoding.UTF8, "application/json");
return response;
}

private static List<LogData> GetLogDatas(LogRequestInfo logRequestInfo)
{
var logs = LogManager.GetLogEntries(DateTime.Parse(logRequestInfo.DateFrom),
DateTime.Parse(logRequestInfo.DateTo),
logRequestInfo.Severity == "Verbose", int.Parse(logRequestInfo.Amount));

var res = logs.Select(logEntry => new LogData()
{
Title = logEntry.Title,
Message = logEntry.Message,
TimeStamp = logEntry.TimeStamp.ToLongDateString(),
Severity = logEntry.Severity
}).ToList();
return res;
}
}

public class LogRequestInfo
{
public string DateFrom;

This comment has been minimized.

Copy link
@burningice2866

burningice2866 Oct 26, 2016

Contributor

Keep types of the DTO properties as proper DateTime and int to not risk loosing precision because of too much unnecessary stringifying and parsing.

public string DateTo;
public string Severity;
public string Amount;
}

internal class LogData
{
public string Title;
public string Message;
public string TimeStamp;

This comment has been minimized.

Copy link
@burningice2866

burningice2866 Oct 26, 2016

Contributor

Keep the DTO property type as DateTime to not risk loosing precision because of too much unnecessary stringifying and parsing.

public string Severity;
}

}
13 changes: 6 additions & 7 deletions Website/Global.asax
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="Composite.Core.Routing" %>
<%@ Import Namespace="Composite.Core.WebClient" %>

<%@ Import Namespace="System.Web.Http" %>

<script RunAt="server">
Expand All @@ -11,9 +11,12 @@
{
ApplicationLevelEventHandlers.LogRequestDetails = false;
ApplicationLevelEventHandlers.LogApplicationLevelErrors = true;
ApplicationLevelEventHandlers.Application_Start(sender, e);
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
GlobalConfiguration.Configuration.EnsureInitialized();

This comment has been minimized.

Copy link
@burningice2866

burningice2866 Oct 26, 2016

Contributor

Use the Configure callback to do any configuration, that way you're sure that WebApi handles all the necessary post-configuration.

https://www.asp.net/web-api/overview/advanced/configuring-aspnet-web-api#webhost
https://msdn.microsoft.com/en-us/library/system.web.http.globalconfiguration.configure(v=vs.118).aspx

RegisterRoutes(RouteTable.Routes);
}
Expand All @@ -23,29 +26,25 @@
Routes.RegisterPageRoute(routes);
// If necessary, add the standard MVC route "{controller}/{action}/{id}" after registering the C1 page route
Routes.Register404Route(routes);
}
void Application_End(object sender, EventArgs e)
{
ApplicationLevelEventHandlers.Application_End(sender, e);
}
void Application_BeginRequest(object sender, EventArgs e)
{
ApplicationLevelEventHandlers.Application_BeginRequest(sender, e);
}
void Application_EndRequest(object sender, EventArgs e)
{
ApplicationLevelEventHandlers.Application_EndRequest(sender, e);
}
protected void Application_Error(object sender, EventArgs e)
{
ApplicationLevelEventHandlers.Application_Error(sender, e);
Expand Down
8 changes: 8 additions & 0 deletions Website/WebSite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Http.WebHost, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
<Private>True</Private>
Expand Down

2 comments on commit f8840c9

@burningice2866
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kasravi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, will look into that

Please sign in to comment.