首页 文章

在mvc 6中读取Windows日志事件时出错

提问于
浏览
0

我正在尝试创建一个Web应用程序来读取所有Windows事件日志并显示到网页中 . 我在asp.net mvc 5中尝试了它的工作但是当我试图在asp.net mvc 6应用程序中执行它时显示错误 .

控制器

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using WebApplication1.Models.PaginationExample;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
        EventLog eventLog = new EventLog("Application", "nb-ahja1", "EventLoggingApp");

        var model = new List<Eventlogg>();
        foreach (EventLogEntry log in eventLog.Entries)
        {
            var demo = new Eventlogg();
            demo.Source = log.Source;
            demo.Id = log.EventID;
            demo.Level = log.EntryType.ToString();
            demo.Date = log.TimeGenerated;
            demo.Category = log.Category;
            model.Add(demo);
        }
        return View(model.OrderByDescending(x => x.Date).ToList());
    }


    public ActionResult About()
    {


        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}
}

型号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class Eventlogg
    {
        public string Source { get; set; }
        public int Id { get; set; }
        public string Level { get; set; }
        public DateTime Date { get; set; }
        public string Category { get; set; }

    }
}

查看

@model List<WebApplication1.Models.Eventlogg>
@{
    ViewBag.Title = "Home Page";
}

<div class="container" >

    <table class="table table-hover" id="myTable">
        <tr>
            <td class="col-md-3"><b>Level</b></td>
        <td class="col-md-3"><b>Date</b></td>
        <td class="col-md-3"><b>Source</b></td>
        <td class="col-md-3"><b>Id</b></td>
        <td class="col-md-3"><b>Category</b></td>
        </tr>
        
@foreach (var item in Model)
{
        <tr>
            <td class="col-md-3">@item.Level</td>
            <td class="col-md-3">@item.Date</td>
            <td class="col-md-3">@item.Source</td>
            <td class="col-md-3">@item.Id</td>
            <td class="col-md-3">@item.Category</td>
        </tr>
}
    </table>

    </div>

错误

严重级代码描述项目文件行抑制状态错误CS0246找不到类型或命名空间名称'EventLog'(您是否缺少using指令或程序集引用?)PocDashboard.DNX Core 5.0 C:\ Users \ ahja \ Documents \ Visual Studio 2015 \ Projects \ AteaPoCDashboard \ src \ PocDashboard \ Controllers \ HomeController.cs 19有效

1 回答

  • 0

    基本上,您缺少需要添加到项目中的程序集引用 .

    'EventLog'是system.diagnostics命名空间的一部分,它是System.dll程序集的一部分,请确保在项目中引用此程序集 .

相关问题