-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathLogsController.cs
More file actions
97 lines (89 loc) · 2.92 KB
/
Copy pathLogsController.cs
File metadata and controls
97 lines (89 loc) · 2.92 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using BlogEngine.Core;
using BlogEngine.Core.Data.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Hosting;
using System.Web.Http;
public class LogsController : ApiController
{
[HttpGet]
public IEnumerable<SelectOption> GetLog()
{
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages))
throw new System.UnauthorizedAccessException();
var action = Request.GetRouteData().Values["id"].ToString();
switch (action)
{
case "file":
return GetLogFile();
default:
break;
}
return new List<SelectOption>();
}
[HttpPut]
public HttpResponseMessage PurgeLog()
{
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages))
throw new System.UnauthorizedAccessException();
string fileLocation = HostingEnvironment.MapPath(Path.Combine(BlogConfig.StorageLocation, "logger.txt"));
try
{
File.Delete(fileLocation);
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (UnauthorizedAccessException)
{
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
catch (Exception ex)
{
Utils.Log("Error purging log file", ex);
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
IEnumerable<SelectOption> GetLogFile()
{
string fileLocation = HostingEnvironment.MapPath(Path.Combine(BlogConfig.StorageLocation, "logger.txt"));
var items = new List<SelectOption>();
if (File.Exists(fileLocation))
{
using (var sw = new StreamReader(fileLocation))
{
string line;
string logItem = "";
int count = 1;
while ((line = sw.ReadLine()) != null)
{
if (line.Contains("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"))
{
// new log item
if (!string.IsNullOrEmpty(logItem))
{
var item = new SelectOption();
item.OptionName = "Line" + count.ToString();
item.OptionValue = logItem;
items.Add(item);
logItem = "";
count++;
}
}
else
{
// append line to log item
logItem = logItem + line + "<br/>";
}
}
sw.Close();
return items;
}
}
else
{
return new List<SelectOption>();
}
}
}