简体   繁体   English

使用PowerShell清除大日志文件,方法是逐行删除并在我的日期比较为真时将其停止

[英]Purge with PowerShell a big log file by deleting line by line and stopping it when my date comparison is true

During these days i'm working on different scripts to delete lines into different log files. 在这些日子里,我正在使用不同的脚本将行删除到不同的日志文件中。 But i have still one file that i can't handle because the structure is bit more complex than the others files i have purged. 但是我仍然有一个我无法处理的文件,因为该结构比我清除的其他文件更复杂。 To give you an idea i put here some example lines of that log file. 为了给您一个想法,我在这里放置了该日志文件的一些示例行。

[ 30-10-2017 16:38:07.62 | INFO    | Some text
[ 30-10-2017 16:38:11.07 | INFO    | Some text
[1];Erreur XXXX non-gérée : Some text.
Merci de communiquer some text :
 - Some text again
 - Identifiant : XXXXXXXX-1789-XXXXX-b41b-XXXX. >> Some text
[ 02-11-2017 16:38:11.10 | INFO    | Some text
[ 02-11-2017 16:38:11.10 | INFO    | Some text
[2];88852228343 / some text
[ 03-11-2017 16:38:11.10 | INFO    | Some text
[ 03-11-2017 16:38:11.10 | INFO    | Some text
Other text here
And other one
[ 04-11-2017 16:38:11.10 | INFO    | Some text
[ 04-11-2017 16:38:11.10 | INFO    | Some text

I have a tried something but i think it was not the right way to doing it -> How to catch terminating error and still continue my PowerShell script 我尝试了一些东西,但我认为这不是正确的方法-> 如何捕获终止错误并仍然继续执行PowerShell脚本

The file is 4Mo and the code to do it right now is not working because i do it the wrong way. 该文件是4Mo,现在执行此操作的代码不起作用,因为我执行错误的方式。 I do first a substring to extract date then compare but when i hit a line which is not starting by a date i have an error and the script stop 我首先执行一个子字符串以提取日期,然后进行比较,但是当我碰到不是以日期开头的行时,我出现了错误并且脚本停止了

try
    {
        (Get-Content $Fichier) |
            Where-Object {$_} |
            Where-Object { ([datetime]::ParseExact(([string]$_).Substring(2,19), $Format, $Culture) -ge (Get-Date).AddDays(-$Jours)) } |
            Set-Content $Fichier
        LogMessage -Message "Fichier $Fichier purgé des toutes les lignes datant de plus de $Jours jours"
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        LogMessage -Message "$FailedItem - $ErrorMessage"
    }

I think it will be better if i start reading file and start deleting each line and then stop to delete the rest of the file when i find a date which is greater or equal than (today - 20 days). 我认为如果我开始读取文件并开始删除每一行,然后在发现大于或等于(今天-20天)的日期时停止删除文件的其余部分,那就更好了。 But right now i can't find a proper way to achieve it in PowerShell. 但是现在我找不到在PowerShell中实现它的正确方法。

Your suggested approach of parsing each line until you find a date that's 20 days or less old would probably work. 您建议的解析每一行直到找到不超过20天的日期的方法都可能会起作用。 I'd do something like this: 我会做这样的事情:

# Placeholder for the dates we're about to parse
$DT = Get-Date
# Define the threshold once
$Limit = (Get-Date).AddDays(-$Jours)
# We'll use this flag to keep track of whether we've reached the limit or not
$Keep = $false

(Get-Content $Fichier |ForEach-Object {
    if($Keep){
        $_
    }
    elseif([datetime]::TryParseExact($_.Substring(2,19), $Format, $Culture, $null, [ref]$DT) -and $DT -ge $Limit){
        $Keep = $true
        $_
    }
}) |Set-Content $Fichier

Another idea could be to generate a file for each day and generate a new logfile for the current day. 另一个想法是每天生成一个文件,并为当天生成一个新的日志文件。

A simultaneous export to a log window and file could save you from log loss and also allow you to keep the logsize at a reasonable size without stressing the CPU. 同时导出到日志窗口和文件可以使您免于丢失日志,还可以使日志大小保持合理的大小,而不会给CPU造成压力。

I was at the same problem with logs especially when using RTF and replacing certain strings with icons to beef up the log. 我在日志上遇到了同样的问题,尤其是在使用RTF并用图标替换某些字符串来增强日志的时候。 and make it exportable easily. 并使其易于导出。

There are several ways to generate logs but for my needs I used the way to write one log per day and purge the log window once the day has switched (00:00:00). 有多种生成日志的方法,但出于我的需要,我每天使用一种方法编写日志,并在每天切换(00:00:00)后清除日志窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM