Write log in ActionFilterAttribute in C#
ActionFilterAttribute
public class JinFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
WriteLog(actionContext);
return base.OnActionExecutingAsync(actionContext, cancellationToken);
}
private async void WriteLog(HttpActionContext actionContext)
{
try
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(actionContext.Request.Method.ToString() + " ");
sb.AppendLine(actionContext.Request.RequestUri.OriginalString);
sb.AppendLine(Convert.ToString(actionContext.Request.Content.Headers.ContentLength));
System.IO.Stream stream = await actionContext.Request.Content.ReadAsStreamAsync();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
var reader = new System.IO.StreamReader(stream, encoding);
string result = reader.ReadToEnd();
long l = Convert.ToInt64(actionContext.Request.Content.Headers.ContentLength);
var hrm = actionContext.Request.Content.LoadIntoBufferAsync(l);
actionContext.Request.Content.CopyToAsync(stream);
reader = new System.IO.StreamReader(stream, encoding);
result = reader.ReadToEnd();
result = result.Replace("\r\n", "");
sb.AppendLine(result);
Task<int> t = new Task<int>(() =>
{
int nWords = 0;
string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + this.ToString() + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
try
{
using (System.IO.StreamWriter sw = System.IO.File.AppendText(logPath))
{
sw.WriteLine("" + sb.ToString());
sw.WriteLine("T:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("**************************************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
catch (System.IO.IOException e)
{
using (System.IO.StreamWriter sw = System.IO.File.AppendText(logPath))
{
sw.WriteLine("Exc:" + e.Message);
sw.WriteLine("T:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
sw.WriteLine("**************************************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
return nWords;
});
t.Start();
}catch(Exception e) { }
}
}
In controller
[Route("v{v}/test")]
[HttpPost]
[AllowAnonymous]
[JinFilter]
public async void Imonline_status(string v, [FromUri] Test request, [FromBody] TestA[] req)
{
try
{
return Ok();
}
#if DEBUG
catch (ArgumentException ae)
{
throw ae;
}
#endif
catch (Exception e)
{
return BadRequest();
}
}
public class JinFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
WriteLog(actionContext);
return base.OnActionExecutingAsync(actionContext, cancellationToken);
}
private async void WriteLog(HttpActionContext actionContext)
{
try
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(actionContext.Request.Method.ToString() + " ");
sb.AppendLine(actionContext.Request.RequestUri.OriginalString);
sb.AppendLine(Convert.ToString(actionContext.Request.Content.Headers.ContentLength));
System.IO.Stream stream = await actionContext.Request.Content.ReadAsStreamAsync();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
var reader = new System.IO.StreamReader(stream, encoding);
string result = reader.ReadToEnd();
long l = Convert.ToInt64(actionContext.Request.Content.Headers.ContentLength);
var hrm = actionContext.Request.Content.LoadIntoBufferAsync(l);
actionContext.Request.Content.CopyToAsync(stream);
reader = new System.IO.StreamReader(stream, encoding);
result = reader.ReadToEnd();
result = result.Replace("\r\n", "");
sb.AppendLine(result);
Task<int> t = new Task<int>(() =>
{
int nWords = 0;
string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + this.ToString() + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
try
{
using (System.IO.StreamWriter sw = System.IO.File.AppendText(logPath))
{
sw.WriteLine("" + sb.ToString());
sw.WriteLine("T:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("**************************************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
catch (System.IO.IOException e)
{
using (System.IO.StreamWriter sw = System.IO.File.AppendText(logPath))
{
sw.WriteLine("Exc:" + e.Message);
sw.WriteLine("T:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
sw.WriteLine("**************************************************");
sw.WriteLine();
sw.Flush();
sw.Close();
sw.Dispose();
}
}
return nWords;
});
t.Start();
}catch(Exception e) { }
}
}
In controller
[Route("v{v}/test")]
[HttpPost]
[AllowAnonymous]
[JinFilter]
public async void Imonline_status(string v, [FromUri] Test request, [FromBody] TestA[] req)
{
try
{
return Ok();
}
#if DEBUG
catch (ArgumentException ae)
{
throw ae;
}
#endif
catch (Exception e)
{
return BadRequest();
}
}
评论
发表评论