How do I programmatically or regularly clear the Operational PrintService event log?

匆匆过客 提交于 2019-12-02 01:27:56

I wasn't able to figure out how to do this with existing classes for working with the Event Log, but calling wevtutil from the application seems to be working.

static void Main(string[] args){        
    const string logSource = "Microsoft-Windows-PrintService/Operational";

    /* store print jobs */

    ClearLog(logSource);
}

public static void ClearLog(string logName)
    {            
        var psi = new ProcessStartInfo(
            "wevtutil.exe",
            String.Format("cl {0}", logName));
        psi.Verb = "runas"; // Run as administrator

        using (var p = new Process())
        {
            p.StartInfo = psi;                
            p.Start();              
        }
    }

I set this up to run every hour using the Task Scheduler on our print server and it is working for the time being. Since it is set to run at the highest level, I don't know if "runas" is needed but to clear the log the process does need administration rights.

I realize this has the potential to miss something if a new job hits between the query and clearing the log but we only have about 30 printers and we're not using these numbers for anything more than seeing if there are any we can remove due to low usage.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!